Pechkin将网页转换为pdf c#给出了空的pdf。

时间:2017-07-24 17:41:39

标签: c# wkhtmltopdf

我正在探索Pechkin将网页转换为PDF。我使用过文章:http://ourcodeworld.com/articles/read/366/how-to-generate-a-pdf-from-html-using-wkhtmltopdf-with-c-in-winforms

参考:How to use wkhtmltopdf.exe in ASP.net

当我尝试使用html字符串进行转换时,它可以工作!

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

但是,当我按照“从网站生成PDF”部分时,我会得到空的pdf。

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

有没有人遇到同样的问题?感谢所有帮助/建议。

1 个答案:

答案 0 :(得分:1)

尝试使用

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();

编辑


如果其他人使用此-请在此处阅读我的帖子-非常重要

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

如果遇到错误,请使用此链接对我有帮助-

TuesPechkin unable to load DLL 'wkhtmltox.dll'

感谢-

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