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

时间:2023-01-18 00:26:41

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.

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

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

我有一个使用CGPDFContextCreateWithURL创建PDF的代码,但我正在努力使用quartz来绘制页面。

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

我在网上和网上搜索了很多,但都没用。

Any help is much appreciated!

非常感谢您的帮助!

6 个解决方案

#1


5  

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:

要从HTML生成pdf,需要将HTML呈现到web视图中,并获取web视图的快照,并将它们呈现到图像上下文中。本教程可能有帮助:

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

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

#2


1  

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.

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

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.

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

#3


0  

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

为什么不使用web服务,将HTML页面发送到这个页面并检索pdf文件呢?

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.

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

#4


0  

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.

我自己还没有尝试过,所以我没有提供任何具体的东西,但是我必须想象有一个简单的方法在iPhone上做这个因为成像模型。我将更深入地研究文档。

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.

至于对你的客户进行反击,可能有很多原因让你想把一切都保持在本地。坦白地说,我对我雇来的一个人说他不能胜任这个特殊的任务一点也不高兴。所以,好好想想这个推后。哦,即使你真的推一个网络服务器是一个糟糕的选择。我将进一步研究为什么您首先需要使用HTML。

#5


0  

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.

我从来没有尝试过,所以我不知道它是否能工作,但是如何将HTML加载到UIWebView中,然后让视图自己绘制到PDF环境中呢?如。

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

Then:

然后:

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

#6


0  

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

我这样做:https://*.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.

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

#1


5  

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:

要从HTML生成pdf,需要将HTML呈现到web视图中,并获取web视图的快照,并将它们呈现到图像上下文中。本教程可能有帮助:

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

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

#2


1  

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.

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

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.

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

#3


0  

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

为什么不使用web服务,将HTML页面发送到这个页面并检索pdf文件呢?

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.

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

#4


0  

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.

我自己还没有尝试过,所以我没有提供任何具体的东西,但是我必须想象有一个简单的方法在iPhone上做这个因为成像模型。我将更深入地研究文档。

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.

至于对你的客户进行反击,可能有很多原因让你想把一切都保持在本地。坦白地说,我对我雇来的一个人说他不能胜任这个特殊的任务一点也不高兴。所以,好好想想这个推后。哦,即使你真的推一个网络服务器是一个糟糕的选择。我将进一步研究为什么您首先需要使用HTML。

#5


0  

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.

我从来没有尝试过,所以我不知道它是否能工作,但是如何将HTML加载到UIWebView中,然后让视图自己绘制到PDF环境中呢?如。

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

Then:

然后:

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

#6


0  

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

我这样做:https://*.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.

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