我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
你可以使用WebBrowser控件的另一个技巧,下面是我的完整工作代码
在我的例子中,为文本框控件分配Url
protected void Page_Load(object sender, EventArgs e)
{
txtweburl.Text = "https://www.google.com/";
}
下面是使用线程生成屏幕的代码
protected void btnscreenshot_click(object sender, EventArgs e)
{
// btnscreenshot.Visible = false;
allpanels.Visible = true;
Thread thread = new Thread(GenerateThumbnail);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void GenerateThumbnail()
{
// btnscreenshot.Visible = false;
WebBrowser webrowse = new WebBrowser();
webrowse.ScrollBarsEnabled = false;
webrowse.AllowNavigation = true;
string url = txtweburl.Text.Trim();
webrowse.Navigate(url);
webrowse.Width = 1400;
webrowse.Height = 50000;
webrowse.DocumentCompleted += webbrowse_DocumentCompleted;
while (webrowse.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
在下面的代码中,我下载后保存pdf文件
private void webbrowse_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// btnscreenshot.Visible = false;
string folderPath = Server.MapPath("~/ImageFiles/");
WebBrowser webrowse = sender as WebBrowser;
//Bitmap bitmap = new Bitmap(webrowse.Width, webrowse.Height);
Bitmap bitmap = new Bitmap(webrowse.Width, webrowse.Height, PixelFormat.Format16bppRgb565);
webrowse.DrawToBitmap(bitmap, webrowse.Bounds);
string Systemimagedownloadpath = System.Configuration.ConfigurationManager.AppSettings["Systemimagedownloadpath"].ToString();
string fullOutputPath = Systemimagedownloadpath + Request.QueryString["VisitedId"].ToString() + ".png";
MemoryStream stream = new MemoryStream();
bitmap.Save(fullOutputPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//generating pdf code
Document pdfDoc = new Document(new iTextSharp.text.Rectangle(1100f, 20000.25f));
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(fullOutputPath);
img.ScaleAbsoluteHeight(20000);
img.ScaleAbsoluteWidth(1024);
pdfDoc.Add(img);
pdfDoc.Close();
//Download the PDF file.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ImageExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
你也可以参考我最老的帖子了解更多信息:在asp.net web表单中导航到网页被取消了
其他回答
这取决于您的其他需求。
一个非常简单但不容易部署的解决方案是使用WebBrowser控件加载Html,然后使用Print方法打印到本地安装的PDF打印机。有一些免费的PDF打印机,WebBrowser控件是. net框架的一部分。
编辑: 如果你的Html是XHtml,你可以使用PDFizer来完成这项工作。
2018年的更新,让我们使用标准的HTML+CSS=PDF方程式!
对于html到pdf的需求,有一个好消息。正如这个答案所示,W3C标准css-break-3将解决这个问题……这是一份候选人推荐书,计划在2017年或2018年经过测试后成为正式推荐书。
由于不太标准,有一些解决方案,使用c#插件,如print-css.rocks所示。
到目前为止,似乎最好的免费。net解决方案是TuesPechkin库,它是wkhtmltopdf本机库的包装。
我现在已经使用单线程版本将几千个HTML字符串转换为PDF文件,它似乎工作得很好。它应该也可以在多线程环境中工作(例如IIS),但我还没有对此进行测试。
另外,因为我想使用最新版本的wkhtmltopdf(在编写时为0.12.5),我从官方网站下载了DLL,复制到我的项目根目录,设置copy to output为true,并像这样初始化库:
var dllDir = AppDomain.CurrentDomain.BaseDirectory;
Converter = new StandardConverter(new PdfToolset(new StaticDeployment(dllDir)));
上面的代码看起来完全是“wkhtmltox.dll”,所以不要重命名文件。我使用的是64位版本的DLL。
确保你阅读了多线程环境的说明,因为你只需要在每个应用生命周期中初始化它一次,所以你需要把它放在一个单例或其他东西中。
我之前也在找这个。我遇到了HTMLDOC http://www.easysw.com/htmldoc/,这是一个免费的开源命令行应用程序,它以HTML文件为参数,并从中输出PDF。这在我的副业项目中非常有效,但这完全取决于你真正需要什么。
制作它的公司出售编译后的二进制文件,但您可以免费从源代码下载和编译并使用它。我设法编译了一个最近的版本(1.9版本),我打算在几天内发布它的二进制安装程序,所以如果你感兴趣,我可以在发布它时立即提供它的链接。
编辑(2/25/2014):看起来文档和网站转移到http://www.msweet.org/projects.php?Z1
我强烈推荐NReco。它有免费版和付费版,真的很值得。它在后台使用wkhtmtopdf,但只需要一个程序集。太棒了。
使用示例:
通过NuGet安装。
var htmlContent = String.Format("<body>Hello world: {0}</body>", DateTime.Now);
var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);
免责声明:我不是开发者,只是这个项目的粉丝:)