在 iPhone 上以编程方式从 HTML 生成 PDF

发布于 2024-09-27 16:31:59 字数 241 浏览 0 评论 0原文

我正在寻找一种以编程方式(在 obj-c 中)从本地 html 文件生成 PDF 文件的方法。我根据用户输入动态生成 html,我需要创建 PDF 并将其发送给用户(通过电子邮件)。我在 PDF 生成部分遇到困难。

我有使用 CGPDFContextCreateWithURL 创建 PDF 的代码,但我在使用quartz 绘制页面时遇到了困难。

我在 SO 和互联网上进行了广泛的搜索,但无济于事。

非常感谢任何帮助!

I am looking for a way to programmatically (in obj-c) generate a PDF file from a local html file. I am dynamically generating the html from user inputs, I need to create the PDF and send it to the user (via email). I am having difficulty with the PDF generation portion.

I have the code to create a PDF using CGPDFContextCreateWithURL but I am struggling with drawing the page using quartz.

I have searched extensively on SO as well as the internet to no avail.

Any help is much appreciated!

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

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

发布评论

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

评论(6

夜血缘 2024-10-04 16:31:59

要从 HTML 生成 pdf,您需要将 html 渲染到 Web 视图中,并拍摄 Web 视图的快照,然后将它们渲染到图像上下文中。
该教程可能会有所帮助:

http://www.ioslearner.com/convert-html-uiwebview -pdf-iphone-ipad/

To generate a pdf from an HTML, you need to render the html into a web view, and take snapshots of the web view, and render them into an image context.
The tutorial might be helpful:

http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/

中二柚 2024-10-04 16:31:59

我编写了一小段代码,它从 DTCoreText 获取 NSAttributedString,并将其呈现为分页 PDF 文件。您可以在我的 GitHub 存储库 上找到它。它不会渲染图像或复杂的 html,但它应该适用于大多数用途。另外,如果您熟悉 CoreText,您可以扩展我的 PDF 框架设置器来生成这些项目。

那么它现在要做的就是:给它一个 HTML 字符串,它将使用 DTCoreText 生成一个 NSAttributedString,然后将其渲染为 PDF。它会返回应用程序文档文件夹中保存 PDF 文件的位置。

I've written a little piece of code that takes an NSAttributedString from DTCoreText, and renders it into a paged PDF file. You can find it on my GitHub Repository. It won't render images or complex html, but it should serve for most uses. Plus, if you're familiar with CoreText, you can extend my PDF frame setter to generate these items.

So what it does now: Give it an HTML string, and it will use DTCoreText to generate an NSAttributedString, then render that into a PDF. It hands back the location that it saved the PDF file in the app's Documents folder.

咆哮 2024-10-04 16:31:59

为什么不使用 WebService,将 HTML 页面发送到此并检索 PDF 文件?

这样您就可以使用 iTextSharp 和 C#,大约 2 分钟即可完成。
另外(如果你是邪恶的)你可以存储和查看服务器上的所有数据。

Why not use a WebService, send the HTML page to this and retrieve the PDF-file ?

That way you can use iTextSharp and C#, and you're done in about 2 minutes.
Plus (if you're evil) you can store and see all the data on your server.

倾城°AllureLove 2024-10-04 16:31:59

我自己没有尝试过,所以我没有什么可以提供具体的,但我不得不想象,由于成像模型,在 iPhone 上必须有一种简单的方法来做到这一点。我会更深入地研究文档。

至于拒绝客户,这取决于你,但可能有多种原因希望将所有内容保留在本地。坦率地说,如果我雇用的某人无法完成这项特定任务,我一点也不高兴。因此,请仔细考虑一下这种反击。哦,即使你确实推迟网络服务器也是一个糟糕的选择。我会进一步回顾一下为什么您首先需要 HTML 中的内容。

I haven't tried this myself so i have nothing to offer concrete but I'd have to imagine there has to be an easy way to do this on iPhone due to the imaging model. I'd look deeper into the documentation.

As to pushing back with the client that is up to you but there are probably multiple reasons for wanting to keep everything local. Frankly I would not be pleased at all to here from somebody I hired that he couldn't manage this particular task. So think long and hard about this push back. Oh even if you do push back a webserver is a poor choice. I'd go back a step further and investgate why you need something in HTML in the first place.

无人问我粥可暖 2024-10-04 16:31:59

我从未尝试过这个,所以我不知道它是否有效,但是如何将 HTML 加载到 UIWebView 中,然后使视图将自身绘制到 PDF 上下文中?例如

UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(...)];
[webview loadHTMLString:html baseURL:...];

然后:

- (void)webViewDidFinishLoad:(UIWebView *)webview {
    CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
    [webview.layer drawInContext:pdfContext];
    ...
}

I've never tried this so I have no idea if it'll work, but how about loading the HTML into a UIWebView, and then make the view draw itself into a PDF context? E.g.

UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(...)];
[webview loadHTMLString:html baseURL:...];

Then:

- (void)webViewDidFinishLoad:(UIWebView *)webview {
    CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
    [webview.layer drawInContext:pdfContext];
    ...
}
无需解释 2024-10-04 16:31:59

我按照以下方式做到了: https://stackoverflow.com/a/13342906/448717

为了维护相同内容的比例我必须将 WKWebView 的大小乘以 UIPrinterRenderer 设置的 printableRect 大小的 1.25 倍,因为屏幕点与 PostScript 的不同......我猜。

I made it by following this SO: https://stackoverflow.com/a/13342906/448717

In order to maintain the same content's proportions I had to multiply the size of the WKWebView 1.25 times the printableRect's size set for the UIPrinterRenderer, as the screen points differs from the PostScript's... I guess.

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