HTML 到 PDF 解决方案(处理内容、页眉和页脚)
我正在尝试通过生成基于 HTML 的 PDF 来创建“报告”。
起初,我只是尝试将原始编码的 HTML 写入文档,然后使用 Javascript 打印该文档。然而,这让我几乎无法控制页眉和页脚。
我尝试使用 thead
和 tfoot
元素,它们在大多数浏览器中运行得相当好,但是我无法获得我正在寻找的格式。
目前 - 我正在尝试在 MVC3 中使用 iTextSharp 来开发服务器端解决方案,但是我对如何继续感到有点迷失,因为我没有太多地使用 iTextSharp。
输出的输入和描述:
创建报告时将使用 4 个项目:
- 报告内容 (当前是编码的 HTML,因为我不确定解码是否会更改任何格式)
- 报告标题 (将只是生成的 PDF 的名称)
- 报告标题 (将是显示在每页左上角)
- 报表页脚 (将显示在每页的左下角)
控制器操作:
//This will be accessed by a jQuery Post
[HttpPost]
public FileStreamResult GeneratePDF(string id)
{
//Grab Report Item
ReportClass report = reportingAgent.GetReportById(id);
Document doc = new Document();
//Do I need to decode the HTML or is it possible to use the encoded HTML?
//Adding Headers / Footers
//Best method of returning the PDF?
}
I'm trying to create a "report" by generating a PDF based on HTML.
At first, I simply attempted to write raw encoded HTML to a document and then print that document using Javascript. However, this gave me little to no control involving headers and footers.
I attempted using thead
and tfoot
elements, which worked reasonably well in most browsers, however I wasn't able to get the formatting that I was looking for.
Currently - I am trying to work on a server-side solution using iTextSharp in MVC3, however I am a bit lost as to how to proceed, having not worked with iTextSharp much.
Input and Description of Output:
There will be 4 items used in creating the Report:
- Report Content (which is currently encoded HTML, as I am unsure if decoding will change any formatting)
- Report Title (will simply be the name of the PDF generated)
- Report Header (will be displayed at the upper-left of each page)
- Report Footer (will be displayed at the lower-left of each page)
Controller Action:
//This will be accessed by a jQuery Post
[HttpPost]
public FileStreamResult GeneratePDF(string id)
{
//Grab Report Item
ReportClass report = reportingAgent.GetReportById(id);
Document doc = new Document();
//Do I need to decode the HTML or is it possible to use the encoded HTML?
//Adding Headers / Footers
//Best method of returning the PDF?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
iTextSharp 无法将 HTML 转换为 PDF。这不是它设计的目的。它旨在从头开始创建 PDF 文件,而不是在各种格式之间转换为 PDF。如果您想将 HTML 转换为 PDF,您可以使用 flying-saucer基于
iText
的库。我有 博客 关于如何使用 IKVM.NET 字节码编译器在 .NET 中完成此操作(ikvmc.exe)。因此,您的控制器操作可能看起来类似于:
其中
PdfResult
可以是自定义操作结果,采用原始 HTML 并将 PDF 输出到响应流中:iTextSharp cannot convert HTML to PDF. It's not what it was designed to do. It was designed to create PDF files from scratch, not converting between various formats into PDF. If you want to convert HTML into PDF you could for example use the the flying-saucer library which is based on
iText
. I have blogged about how this could be done in .NET using IKVM.NET Bytecode Compiler (ikvmc.exe).So your controller action might look something along the lines of:
where
PdfResult
could be a custom action result taking the raw HTML and outputting the PDF into the response stream: