将 HTML 转换为 PDF

发布于 2024-08-16 13:15:11 字数 818 浏览 7 评论 0原文

参考之前的帖子(PDF 报告生成),

我决定使用类似于 http://www.alistapart.com/articles/boom

对于那些不这样做的人不想阅读任何参考资料 - 我正在创建一份报告并需要 PDF 格式。我决定使用 HTML ->使用 .NET 的 PDF 路由。

因此,假设我得到的 HTML 文件与我想要的完全一样。将所述页面转换为 PDF 的最佳方法是什么?本质上,我希望用户看到 HTML 中的“预览”,然后能够将所述页面转换为 PDF。我目前正在试验的库是 ABCPdf。

我的第一个想法是将页面保存到文件系统,并在页面本身的事件处理程序期间在转换例程中引用它的 URL。这有问题,因为每次渲染页面时我都必须保存页面以准备打印。事实上,这似乎只是倒退。

我的下一个想法是使用页面的渲染方法将页面捕获为字节流并使用它(因为 ABCPdf 支持转换 HTML 流。)如果这是我的答案,我不知道如何实际实现它。有一个“打印”按钮,该按钮的处理程序对字节流执行 Me.Render() 并将其发送到转换例程?这可能吗?

底线 - 一旦页面以漂亮的 HTML 呈现,您如何初始化该页面到 PDF 的转换?欢迎提供变通方法和其他解决方案。

我希望我错过了一些明显的东西,因为这必须是“简单的部分

In reference to an earlier post (PDF Report generation)

I have decided to use a solution similar to http://www.alistapart.com/articles/boom

For those of you who don't want to read either reference - I'm creating a report and need it as a PDF. I've decided to go the HTML -> PDF route using .NET.

So, let's say I get the HTML file exactly like I want it. What is the best way to convert said page to PDF? In essence, I'd like the user to see a "preview" in HTML and then be able to convert said page to PDF. The library I'm currently experimenting with is ABCPdf.

My first thought was to save the page to the filesystem and reference it's URL in the conversion routine during an eventhandler on the page itself. This has it's problems because I'd have to save the page each time it was rendered in preparation to print it. Actually, it just seems backasswards.

My next thought was to use the page's render method to capture the page as a bytestream and use this (since ABCPdf supports converting a stream of HTML.) If this is my answer, I'm lost at how to actual pull it off. Have a "Print" button that's handler does a Me.Render() to bytestream and send that to the conversion routine? Is that even possible?

Bottom line - Once a page is rendered in nice HTML, how do you initialize a conversion to PDF of that page? Workarounds and other solutions are welcome.

I'm hoping I'm missing something obvious as this has got to be "the easy part"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐籹人 2024-08-23 13:15:11

好的,开始工作了——而且相当简单。只是将其传递给下一个可能需要答案的人。我只是使用了页面的 Url 属性并将其发送到 ABCPdf addImageUrl() 方法。还必须使用链接,因为它不止一页。感谢您的所有帮助。

Dim oPdfDoc As New Doc()
Dim iPageID As Int32
Dim MyUrl = Request.Url

iPageID = oPdfDoc.AddImageUrl(MyUrl.AbsoluteUri)

While True
    oPdfDoc.FrameRect()
    If Not oPdfDoc.Chainable(iPageID) Then
        Exit While
    End If
    oPdfDoc.Page = oPdfDoc.AddPage()
    iPageID = oPdfDoc.AddImageToChain(iPageID)
End While

For i as Int32 = 1 To oPdfDoc.PageCount
    oPdfDoc.PageNumber = i
    oPdfDoc.Flatten()
Next

oPdfDoc.Save(Server.MapPath("test.pdf"))
oPdfDoc.Clear()

Ok, got it working - and it was fairly simple. Just passing this along to the next guy who might need the answer. I just used the Url property of the page and sent it to the ABCPdf addImageUrl() method. Also had to use chaining since it was more than one page. Thanks for all of the help.

Dim oPdfDoc As New Doc()
Dim iPageID As Int32
Dim MyUrl = Request.Url

iPageID = oPdfDoc.AddImageUrl(MyUrl.AbsoluteUri)

While True
    oPdfDoc.FrameRect()
    If Not oPdfDoc.Chainable(iPageID) Then
        Exit While
    End If
    oPdfDoc.Page = oPdfDoc.AddPage()
    iPageID = oPdfDoc.AddImageToChain(iPageID)
End While

For i as Int32 = 1 To oPdfDoc.PageCount
    oPdfDoc.PageNumber = i
    oPdfDoc.Flatten()
Next

oPdfDoc.Save(Server.MapPath("test.pdf"))
oPdfDoc.Clear()
謸气贵蔟 2024-08-23 13:15:11

我的情况和你一样,在评估了很多选项(包括 iTextSharp 和 ABC PDF)之后,我最终得到了 wkhtmltopdf: http://code.google.com/p/wkhtmltopdf/

I was in the same situation as you and after evaluating a lot of options including iTextSharp and ABC PDF, I ended up with wkhtmltopdf: http://code.google.com/p/wkhtmltopdf/.

灼疼热情 2024-08-23 13:15:11

你如何从 C# 中做到这一点?你不(直接)。

生成 PRINCE.EXE 工作进程可能是您唯一的选择。

PRINCE.EXE 将从 HTML“标准输入”读取/写入数据并将 PDF 发送到“标准输出”。使用命令行“%dir%\PRINCE -”,不带输出文件名。

您可能会发现需要一个单独的 COM 组件来生成 PRINCE,因为 System.Management 类可能不适合您。使用 Visual Basic 或 C++ 来制作 COM 组件。

通常,将 HTML 放入数据库是一个坏主意,但在您的情况下可能没问题,因为听起来它本质上是静态的。

编辑

将“子 PRINCE.EXE”更改为“工作 PRINCE.EXE 进程”。我有一种有趣的感觉,PRINCE.EXE 不需要是一个子进程。

How do you do it from C#? You don't (directly).

Spawning a worker PRINCE.EXE process may be your only option.

PRINCE.EXE will read/write data from HTML "standard input" and sent PDF to "standard output". Use the command line "%dir%\PRINCE -" with no output file name.

You may find you need a separate COM component to spawn PRINCE, as the System.Management class might not work for you. Use Visual Basic or C++ to make your COM component.

Putting HTML in the database is a bad idea generally, but may be okay in your case, as it sounds like it's essentially static.

EDIT

Changed "child PRINCE.EXE" to "worker PRINCE.EXE process". I have a funny feeling that PRINCE.EXE needs to not be a child process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文