PDF Kit, to keep a copy of PDF files generated

206 views
Skip to first unread message

Harisankar P S

unread,
Aug 27, 2012, 12:32:26 AM8/27/12
to bangal...@googlegroups.com
Hello there,

Do anyone have experience working with the gem pdfkit (https://github.com/pdfkit/pdfkit), I am using this gem to create pdf from html pages. In my application ( based on rack) I use it as a middle where.

All goes well.. typing http://localhost:9292/invoice.pdf generates the PDF file of that page. I wish to store a copy of this PDF file generated int the server as well.
Have anyone else tried doing it, if so how did you achieve it?


--
Harisankar P S


Jasim A Basheer

unread,
Aug 27, 2012, 6:03:52 AM8/27/12
to bangal...@googlegroups.com
You can use Ruby's File class to create and save file in any directory on the server. Ensure that it is not accessible directly (set appropriate unix file permissions and put it outside any publicly accessible folder).




--
Harisankar P S


--
 
 



--
Regards,
Jasim A Basheer.
C42 Engineering, http://c42.in
http://jasimabasheer.com

Harisankar P S

unread,
Aug 27, 2012, 6:17:30 AM8/27/12
to bangal...@googlegroups.com
How you add extra functionality to a middleware to add this functionality. Ie that is to save a copy of the pdf file created into a folder.

--
 
 



--
Harisankar P S
http://tech.hsps.in

Jasim A Basheer

unread,
Aug 27, 2012, 7:21:17 AM8/27/12
to bangal...@googlegroups.com
You have to first create a PDF file in a temporary directory to be able to use send_file (http://apidock.com/rails/ActionController/DataStreaming/send_file), if you are using it.

Can you pastie the code of your middleware that sends the PDF to the client?

Harisankar P S

unread,
Aug 27, 2012, 7:38:41 AM8/27/12
to bangal...@googlegroups.com

I am not using rails. i am building it using cuba ( a rack based framework)

https://gist.github.com/3487744



--
Harisankar P S

Jasim A Basheer

unread,
Aug 27, 2012, 8:40:08 AM8/27/12
to bangal...@googlegroups.com
This is pretty specific to PDFKit. I just took a look at the Middleware. It seems it is generating the PDF in-memory and sending it directly across to Rack. Check this: https://github.com/pdfkit/pdfkit/blob/master/lib/pdfkit/middleware.rb#L11

If you need to be able to save them to your machine, you can write your own Middleware (you can copy-paste from pdfkit#middleware.rb, and customize it by adding File handling routines immediately after the PDF is generated - around line 21 in the above URL).

However, let me ask the dreaded question: why do you need to do this? Having PDF generated automatically for every URL the user visits does not seem to be a common use case. Unless you are playing around with it.

Let us know how it goes or if you need more help.






--
Harisankar P S

--
 
 

Kashyap Kondamudi

unread,
Aug 27, 2012, 2:16:02 PM8/27/12
to bangal...@googlegroups.com
Hari,

27 August 2012 10:02 AM
Hello there,

Do anyone have experience working with the gem pdfkit (https://github.com/pdfkit/pdfkit), I am using this gem to create pdf from html pages. In my application ( based on rack) I use it as a middle where.

All goes well.. typing http://localhost:9292/invoice.pdf generates the PDF file of that page. I wish to store a copy of this PDF file generated int the server as well.
Have anyone else tried doing it, if so how did you achieve it?


--
Harisankar P S


--
 
 


Perhaps you might've seen this already: Wicked PDF. Uses the same backend that PDFkit uses - wkhtmltopdf but has a lot neater documentation and structure IMO. I've used both Prawn and PDFkit for some of my pet projects. Had to chose PDFkit over Wicked PDF since I mainly use Sinatra and so the "respond_to" style usage that WickedPDF provides requires another Sinatra extension.


Hope that helps

--
Kashyap

Harisankar P S

unread,
Aug 28, 2012, 3:56:59 AM8/28/12
to bangal...@googlegroups.com
Am not trying to make every link changed to pdf, its a simple application that help to generate invoice and the invoice page has a link to itself that point to /invoice.pdf.

I was just trying out the possibility since the PDF is already being generated, why not store a copy of it in the server or a backup be send via email to the accounts.

Thanks for the help, let me see if I can add that file storage functionality to it. I will work on it this weekend and let you know how it turns out..

--
Harisankar P S

Vinay Shivanna

unread,
Aug 28, 2012, 7:32:13 AM8/28/12
to bangal...@googlegroups.com
Hari,

Recently i did a research on pdf generation using html as the input and found out wicked pdf is a good out of the availables.  It uses wkhtmltopdf to generate pdf from html file. make sure u have installed this library.

Link for wkhtmltopdf : http://code.google.com/p/wkhtmltopdf/

There are little rendering problems here and their, but you can rectify them. Let me know if this helps you.

Regards
Vinay S

--
 
 

compose-unknown-contact.jpg
Reply all
Reply to author
Forward
0 new messages