1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > .net html转为pdf 在.NET中将HTML转换为PDF

.net html转为pdf 在.NET中将HTML转换为PDF

时间:2019-08-02 13:16:22

相关推荐

.net html转为pdf 在.NET中将HTML转换为PDF

我想通过将HTML内容传递给函数来生成PDF。 我已经为此使用了iTextSharp,但是当它遇到表格并且布局变得凌乱时,它的表现不佳。

有没有更好的办法?

#1楼

尝试使用此PDF Duo .Net转换组件,可将HTML从应用程序转换为PDF,而无需使用其他dll。

您可以传递HTML字符串或文件,或流以生成PDF。 使用下面的代码(示例C#):

string file_html = @"K:\hdoc.html";

string file_pdf = @"K:\new.pdf";

try

{

DuoDimension.HtmlToPdf conv = new DuoDimension.HtmlToPdf();

conv.OpenHTML(file_html);

conv.SavePDF(file_pdf);

textBox4.Text = "C# Example: Converting succeeded";

}

#2楼

好的,使用这项技术。

src可以从这里下载,需要nant

#3楼

更新:现在我推荐在wkhtmltopdf上使用PupeteerSharp。

尝试wkhtmtopdf 。 这是到目前为止我发现的最好的工具。

对于.NET,您可以使用此小型库轻松调用wkhtmtopdf命令行实用程序。

#4楼

您可以创建HTML页面的位图,然后使用例如iTextSharp将位图插入PDF,而不是直接将HTML解析为PDF。

这是一个如何获取URL位图的代码。 我在SO的某处找到了它,如果找到了源,我将其链接起来。

public System.Drawing.Bitmap HTMLToImage(String strHTML)

{

System.Drawing.Bitmap myBitmap = null;

System.Threading.Thread myThread = new System.Threading.Thread(delegate()

{

// create a hidden web browser, which will navigate to the page

System.Windows.Forms.WebBrowser myWebBrowser = new System.Windows.Forms.WebBrowser();

// we don't want scrollbars on our image

myWebBrowser.ScrollBarsEnabled = false;

// don't let any errors shine through

myWebBrowser.ScriptErrorsSuppressed = true;

// let's load up that page!

myWebBrowser.Navigate("about:blank");

// wait until the page is fully loaded

while (myWebBrowser.ReadyState != System.Windows.plete)

System.Windows.Forms.Application.DoEvents();

myWebBrowser.Document.Body.InnerHtml = strHTML;

// set the size of our web browser to be the same size as the page

int intScrollPadding = 20;

int intDocumentWidth = myWebBrowser.Document.Body.ScrollRectangle.Width + intScrollPadding;

int intDocumentHeight = myWebBrowser.Document.Body.ScrollRectangle.Height + intScrollPadding;

myWebBrowser.Width = intDocumentWidth;

myWebBrowser.Height = intDocumentHeight;

// a bitmap that we will draw to

myBitmap = new System.Drawing.Bitmap(intDocumentWidth - intScrollPadding, intDocumentHeight - intScrollPadding);

// draw the web browser to the bitmap

myWebBrowser.DrawToBitmap(myBitmap, new System.Drawing.Rectangle(0, 0, intDocumentWidth - intScrollPadding, intDocumentHeight - intScrollPadding));

});

myThread.SetApartmentState(System.Threading.ApartmentState.STA);

myThread.Start();

myThread.Join();

return myBitmap;

}

#5楼

使用Winnovative HTML转PDF转换器,您可以在一行中转换HTML字符串

byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlString, baseUrl);

基本URL用于解析HTML字符串中相对URL引用的图像。 另外,您可以在HTML中使用完整的URL或使用src =“ data:image / png”嵌入图像作为图像标签。

为了回答有关Winnovative转换器的“ fubaar”用户评论,必须进行更正。 该转换器不使用IE作为渲染引擎。 它实际上不依赖于任何已安装的软件,并且呈现与WebKit引擎兼容。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。