Search code examples
c#wkhtmltopdf

Pechkin converting webpage to pdf c# gives empty pdf.


I am exploring Pechkin to convert webpage to PDF. I have used article: http://ourcodeworld.com/articles/read/366/how-to-generate-a-pdf-from-html-using-wkhtmltopdf-with-c-in-winforms

Ref: How to use wkhtmltopdf.exe in ASP.net

When i try to convert using html string, it works !

byte[] pdfContent = new SimplePechkin(new GlobalConfig()).Convert("<html><body><h1>Hello world!</h1></body></html>");

However when I follow "Generate PDF from a Website" section, I get empty pdf.

configuration.SetCreateExternalLinks(false)
    .SetFallbackEncoding(Encoding.ASCII)
    .SetLoadImages(true)
    .SetPageUri("http://ourcodeworld.com");

Has anyone encountered same issue? Appreciate all help/suggestions.


Solution

  • Try to use

    https://github.com/tuespetre/TuesPechkin

    var document = new HtmlToPdfDocument
    {
        GlobalSettings =
        {
            ProduceOutline = true,
            DocumentTitle = "Pretty Websites",
            PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
            Margins =
            {
                All = 1.375,
                Unit = Unit.Centimeters
            }
        },
        Objects = {
            new ObjectSettings { HtmlText = "<h1>Pretty Websites</h1><p>This might take a bit to convert!</p>" },
            new ObjectSettings { PageUrl = "www.google.com" },
            new ObjectSettings { PageUrl = "www.microsoft.com" },
            new ObjectSettings { PageUrl = "www.github.com" }
        }
    };
     var tempFolderDeployment = new TempFolderDeployment();
     var win32EmbeddedDeployment = new Win32EmbeddedDeployment(tempFolderDeployment);
     var remotingToolset = new RemotingToolset<PdfToolset>(win32EmbeddedDeployment);
    
     var converter = ThreadSafeConverter(remotingToolset);
     byte[] pdfBuf = converter.Convert(document);
     // Very important - overwise cpu will grow !!!
     remotingToolset.Unload();
    

    Edit


    If someone else use this- please read my post here- very important!

    https://stackoverflow.com/a/62428122/4836581

    If you get errors use this link that help me-

    TuesPechkin unable to load DLL 'wkhtmltox.dll'

    Found it thanks to-

    https://stackoverflow.com/a/26993484/4836581