views:

91

answers:

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!

+1  A: 

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.

Quandary
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
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
Quandary
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).
Quandary
+1  A: 

have you seen this tutorial, it was helpful to me

http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/10989-pdf-creation-tutorial.html

Aaron Saunders
I have seen this tutorial, it was pretty helpful. I think I will just push back and demand we just create a web service. Thanks for the quick response.
JWD
A: 

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.

David Frantz
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
@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
A: 

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
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