Rails PDFKit wait for client side
Asked Answered
L

2

7

I have successfully implemented PDFKit into my Rails application. But my application is mostly client side. I have lots of javascript widgets and it takes around 3 seconds for all of them to load. When I generate the pdf I can see some of the widgets have loaded, some are in the process of loading, and others have not loaded at all.

Is there some way to work around this? It doesn't have to be too fancy. If I can delay the PDF generation for 5 seconds, until the page has completed loaded, that's fine.

Lucerne answered 29/9, 2013 at 11:26 Comment(0)
S
8

PDFKit uses wkhtmltopdf to generate the pdfs based on a webkit render of the page. There is a --javascript-delay params that looks like it could be used to delay the generation for the client side to finish.

Try this in pdfkit.rb

config.default_options = {
  :javascript_delay => 5000
} 
Serviceman answered 29/9, 2013 at 21:56 Comment(1)
Works for wicked_pdf too!Linhliniment
Q
0

You can tell pdfkit to wait until window.status JS variable gets a specific value.

PDFKit.new(
  ...,
  window_status: 'ready',
  ...
)

Thus within your javascript you can flag for PDFKit it's time to generate PDF by doing:

window.status = 'ready'

Simple as this :D

Not waiting more than needed. Not risking generating PDF before wished.

Quintal answered 27/3, 2018 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.