4

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!

JWD
  • 12,188
  • 3
  • 34
  • 32

6 Answers6

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:

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

Bani Uppal
  • 866
  • 9
  • 17
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.

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.

Neysor
  • 3,893
  • 11
  • 34
  • 66
OC Rickard
  • 1,067
  • 7
  • 9
0

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.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • I originally thought to use a web service, the client wanted to keep the app totally isolated. It is looking more like I am going to go this route, it is just too much easier. Thanks for the quick response. – JWD Oct 14 '10 at 22:11
  • If your client wants to keep the data confidential, use SSL, and if that's not enough, the web-service must run on the client's server. You may also find this interesting in this context: http://stackoverflow.com/questions/3787386/howto-ignore-ssl-certificate-error-in-webservice-request – Stefan Steiger Oct 14 '10 at 22:15
  • PS: Actually a simple HTTP(S) post would be much easier than a webservice, if you call it from Objective-C. That you can handle in a generic handler (ashx). – Stefan Steiger Oct 14 '10 at 22:17
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.

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.

  • There are other reasons to take it online other than this particular issue. Also, pushing it to a web server is not at all a poor choice, try to stick to answering the questions asked. – JWD Oct 15 '10 at 03:37
  • @JWD: He is sticking to the question. You asked how to generate a PDF from a local HTML file and you tagged it iPhone which implies on the iPhone. You never said "server side solutions are acceptable". – JeremyP Oct 15 '10 at 10:08
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.

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

Then:

- (void)webViewDidFinishLoad:(UIWebView *)webview {
    CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
    [webview.layer drawInContext:pdfContext];
    ...
}
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • Thanks for the input, I am giving this a try but am only coming up with a blank (white) pdf. I may be missing something here though. – JWD Oct 15 '10 at 03:35
0

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.

Community
  • 1
  • 1
Jim75
  • 737
  • 8
  • 13