Generate a PDF within an app

Hello everyone,

I’m working on a business app that gets input from a user, does some calculations, then generates a PDF report from those calculations. I need a method to create a PDF within the app.

I found a method to do this using a server and PHP. So the app sends a JSON file to a server. The server uses PHP to create a PDF then sends the PDF back to the app.

What I would like is a way to do this entirely within the app. Does anyone know a method to do this?

Thanks in advance,

Nate

Well, theoretically you could - it’s a binary file. If your reports are all very similar, you might be able to do it with a template pdf file and replacing bits. Years ago I remember creating an old Windows Help File format using this method, but it’s a complete hack.  The idea is that you put a really obvious marker ****IAMYOURMARKER**** in the file and then use gsub to replace it. This is dependent on things like PDF being text - I think OTOMH it is, but it is often compressed.

Wouldn’t it be better to (say) generate HTML and CSS rather than the PDF file ? Or just stick with the server version ?

Failing that, you could convert one of the PHP ones - the languages are similar enough to make it doable, albeit it would be very dull. In fact, looking at fpdf.php you could probably write a script to convert it, or most of it. So $this-> becomes self. and so on, null becomes nil. Not sure how practical this is :slight_smile:

Thanks for the ideas Paul!

Ideally, I’d like to not rely on a server if possible. And, HTML/CSS would be great but the output must be in PDF form. 

Converting fpdf.php would be incredibly dull. :slight_smile: What would be really nice is to find a plugin that is already built! Dreams come true sometimes, right? 

They do exist for lua - but they are almost all wrappers for other pdf creating libraries - I did have a quick look. I think if you are an enterprise customer you can do this, but if you aren’t it would either have to be ‘raw’ lua or server side.

I don’t think the conversion would be too difficult, much of the difference is syntactic. So you’d be (for example) replacing $this-> with self. and so on. A fair chunk of it would have to be done by hand, but not all of it. It would also help if it was limited - you could only convert the functionality you actually needed.

Oh nice! Where did you find the plugins?

I haven’t been able to find any. I would be willing to upgrade to Enterprise to be able to use them.

I just Google’d “lua pdf library” and found LuaHPDF at http://luahpdf.colloquiki.com/. Is this the sort of thing you were talking about?

Well, theoretically you could - it’s a binary file. If your reports are all very similar, you might be able to do it with a template pdf file and replacing bits. Years ago I remember creating an old Windows Help File format using this method, but it’s a complete hack.  The idea is that you put a really obvious marker ****IAMYOURMARKER**** in the file and then use gsub to replace it. This is dependent on things like PDF being text - I think OTOMH it is, but it is often compressed.

Wouldn’t it be better to (say) generate HTML and CSS rather than the PDF file ? Or just stick with the server version ?

Failing that, you could convert one of the PHP ones - the languages are similar enough to make it doable, albeit it would be very dull. In fact, looking at fpdf.php you could probably write a script to convert it, or most of it. So $this-> becomes self. and so on, null becomes nil. Not sure how practical this is :slight_smile:

Thanks for the ideas Paul!

Ideally, I’d like to not rely on a server if possible. And, HTML/CSS would be great but the output must be in PDF form. 

Converting fpdf.php would be incredibly dull. :slight_smile: What would be really nice is to find a plugin that is already built! Dreams come true sometimes, right? 

They do exist for lua - but they are almost all wrappers for other pdf creating libraries - I did have a quick look. I think if you are an enterprise customer you can do this, but if you aren’t it would either have to be ‘raw’ lua or server side.

I don’t think the conversion would be too difficult, much of the difference is syntactic. So you’d be (for example) replacing $this-> with self. and so on. A fair chunk of it would have to be done by hand, but not all of it. It would also help if it was limited - you could only convert the functionality you actually needed.

Oh nice! Where did you find the plugins?

I haven’t been able to find any. I would be willing to upgrade to Enterprise to be able to use them.

I just Google’d “lua pdf library” and found LuaHPDF at http://luahpdf.colloquiki.com/. Is this the sort of thing you were talking about?