PdfSharp、GDI+ 和 HTML 打印
我目前有一个“PrintingWebService”,我从 AJAX 页面调用它,其中包含使用 PDF Sharp 和 PDFSharp 的 GDI+ 模式构造高度定制的 PDF 打印输出所需的所有信息,该模式采用 DrawString 和其他基本上与 GDI+ 一样工作的命令他们被 PDF 所吸引。
然后,我将 PDF 文件保存到 Web 服务器上的某个位置,并从 Web 服务返回文件名,AJAX 页面将打开一个包含 pdf 文件的新窗口。
到目前为止,它运行良好,但是,我想打印 AJAX 页面的一部分,但我还没有想出解决方案。 我有一个 TinyMCE 编辑器的 HTML 内容字符串,我想将其显示在 PDF 页面的底部。
我正在寻找某种可以用于此目的的工具。 即使是打印到 GDI+ 的开源代码,我也可以通过获取源代码并将其转换为使用 PdfSharp 的 GDI+ 来使用(类名称类似于 XGraphics,每个类在 GDI+ 名称之前都有 X)。
如果必须的话,我会限制 TinyMCE 可以生成的 HTML 并编写自己的渲染器,但这将是一个很大的挑战,所以我首先寻找其他解决方案。
我一直远离打印机友好的页面方法,因为我想使用现有代码构建一个与现有 WinForms 打印输出几乎相同的页面。 使用 PdfSharp,我能够转换除文本区域内容(在 WinForms 版本中使用 RichTextBox 和 RTF)之外的所有代码。
I currently have a "PrintingWebService" that I call from an AJAX page with all the information that is needed to construct a highly customized PDF printout using PDF Sharp and the PDFSharp's GDI+ mode, which takes DrawString and other commands that work basically just like GDI+ only they are drawn to the PDF.
I then save the PDF file to a location on the webserver and return the file name from the web service, and the AJAX page opens a new window with the pdf file.
So far, it works well, however, there is one part of my AJAX page that I want to printout and I haven't come up with a solution for yet. I've got a string of the HTML content of a TinyMCE editor that I want to dispay in the bottom part of the PDF page.
I'm looking for some sort of tool I could use for this purpose. Even something opensource that prints to GDI+ I could use by taking the source code and translating it to use PdfSharp's GDI+ (the class names are like XGraphics, with each class having X before the GDI+ name).
If I have to I will limit what HTML can be generated by TinyMCE and write my own renderer, but that will be a big challenge, so I'm looking for other solutions first.
I've stayed away from a printer-friendly page approach because I wanted to construct a page that was a near identical of an existing WinForms printout, using my existing code. With PdfSharp I was able to convert all the code except the text area stuff (which used the RichTextBox and RTF in the WinForms version).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
托尼,
我个人已经使用了 WebSupergoo 的 ABCPdf 库并取得了很大成功。 实际上,您可以将 HTML 直接渲染为 PDF,而且它在准确性方面表现相当不错。
另一个免费软件 iTextSharp 可以让您灵活地将 HTML 写入 PDF,我过去曾使用过它并取得了很大成功。
否则,我认为你必须编写一些东西来将 HTML 渲染到 GDI。
无论哪种方式,您可能需要考虑使用您映射到使用 web.config 的 HttpHandler 来生成 PDF 文件。 这将允许您将 PDF 呈现为字节流,然后将其直接转储给用户(而不是必须将每个 PDF 收据保存到 Web 服务器)。 它还允许您在返回收据的页面中使用 .pdf 扩展名(PurchaseReceipt.pdf 可以映射到 HttpHandler)...使其更加跨浏览器友好。 如果您开始从 ASPX 页面抛出 PDF 字节流,旧版本的 Adobe/浏览器将无法正确显示。
希望这可以帮助。
Tony,
I personally have used WebSupergoo's ABCPdf library with much success. You can actually render HTML directly to the PDF and it does fairly well in regards to accuracy.
Another free software that will allow you the flexibility of writing HTML to PDF that I have used in the past with much success is iTextSharp.
Otherwise, I think you'll have to write something to render HTML to GDI.
Either way, you may want to consider using an HttpHandler that you map to using your web.config to generate the PDF file. This will allow for you to render the PDF to a bytestream and then dump it directly to the user (as opposed to having to save each PDF receipt to the web server). It will also allow for you to use the .pdf extension in the page that returns the receipt (PurchaseReceipt.pdf could be mapped to a HttpHandler)... making it more cross-browser friendly. Older versions of Adobe / Browsers will not display correctly if you start throwing a PDF byte stream from an ASPX page.
Hope this helps.