HTML to PDF conversion library?

358 views
Skip to first unread message

Matt Freedman

unread,
Nov 4, 2009, 5:41:01 PM11/4/09
to iPhone SDK Development
Is anybody aware of a way to convert an html page to pdf in code that
runs on the iPhone? I have gotten UIWebView to render into a pdf file,
but that just creates a bitmap, I would really prefer a normal pdf,
with selectable/searchable text.

I have a found a few libraries around for this, but so far mostly Java
and .Net. Is there anything that could actually be embedded in an app?
Preferably open source, but commercial might be ok as well.

-- Matt

mariot

unread,
Nov 5, 2009, 12:49:08 AM11/5/09
to iPhone SDK Development
I'm also looking for one.

By the way, How were you able to render a pdf file (bitmap) from a
UIWebView?
Did you also use a library for this?

Matt Freedman

unread,
Nov 5, 2009, 12:38:10 PM11/5/09
to iphonesdkd...@googlegroups.com
I started with this tutorial on creating pdf's on the iPhone...

Then I took out the code there for drawing into the pdf, and replaced it with this:
// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
// before we start drawing.
CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
CGContextScaleCTM(pdfContext, 1.0, -1.0);

// webView is my UIWebView component

[webView.layer renderInContext:pdfContext];

But this creates a very small bitmap, i.e. the size of the iPhone screen. I have been experimenting with setting the frame of the UIWebView to be much larger, as well as with making the mediaBox parameter of the pdf (the &pageRect in the tutorial sample) larger, but I have not had success in generating a large bitmap of a complete html page.

But really, bitmap just seems like a bad approach. For the text to be readable (and print well) on a desktop, the bitmap would have to be extremely high resolution. Even if I could get it to work, it seems like I might run into memory and file size issues. A "real" pdf with selectable, scalable text would be far better.

I have gotten a few responses on this topic on the Apple developer forum, but nothing really solid...

It looks like this is the best of the commercial html to pdf converters...

I am currently in an email dialog with somebody from that company about them possibly porting their library to iPhone (they already have MacOS and variety of unixes). But it is not yet clear how feasible that would be for them, or how much licensing would be. But I will introduce you to him in a separate email.

-- Matt

mariot

unread,
Nov 5, 2009, 9:30:59 PM11/5/09
to iPhone SDK Development
Thanks Matt.

UIWebview seems to use CATiledLayer. I think that's the reason why you
are
getting a very small bitmap.
http://stackoverflow.com/questions/469764/rendering-a-uiwebview-into-an-imagecontext


On converting HTML to PDF, I guess you also need to consider the
images embedded on
the webpage. Take note that an HTML file purely contains text (tags,
image URL, etc.)
Reply all
Reply to author
Forward
0 new messages