在客户端使用HTML创建PDF [英] Creating PDF out of HTML at client side
本文介绍了在客户端使用HTML创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的MVC.net(C#)应用程序中,我正在使用JQplot创建在客户端创建报告。报告生成位于页面上的占位符('div'元素)内。
我的要求是我需要创建一个包含我页面所有数据的PDF,例如页面( HTML)可能在文本和报告中有报告描述。所有这些HTML都需要输入PDF文件。
简而言之:整个HTML必须用PDF文件写成。
如何做到这一点?
In my MVC.net (C#) application, I am creating creating reports at client side using JQplot. The report generate is placed inside a placeholder on the page ('div' element).
My requirement says that I need to create a PDF having all the data of my page, for-example the page (HTML) may have report description in text and the report. All this HTML need to go in PDF file.
In brief : Entire HTML has to be written in PDF file.
How this can be done?
推荐答案
myHTMLTable.Border = 1;
Response.ContentType =application / pdf ;
Response.AddHeader(content-disposition,attachment; filename = ReportName.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
/ /lblReportName.Visible = true;
hw.WriteLine( Header Text ); //将标题添加到pdf文件
pnlPerson.RenderControl(hw); /// pnlPerson是你的HTML表格内部的面板
StringReader sr = new StringReader(sw.ToString());
文件pdfDoc =新文件(PageSize.A4,10f,10f,5f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
为此,你必须使用3rd派对工具iTextSharp>
这可以帮助
myHTMLTable.Border = 1;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ReportName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//lblReportName.Visible = true;
hw.WriteLine("Header Text"); // add header to pdf file
pnlPerson.RenderControl(hw); ///pnlPerson is panel inside that your HTML table will come
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 5f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
For this you have to use 3rd party tool iTextSharp>
May this will help
这篇关于在客户端使用HTML创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文