Search code examples
ruby-on-railsrubywkhtmltopdfprawn

Rails Prawn Pdf gem suggestions needed


I have been using wkhtmltoppdf in my application for generating pdf's.Since now I have observed it's not supporting files over 1000+ pages. I am interested to give try to prawn pdf, as it seems faster than other gems. Could anyone tell, anyway we have in prawn gem to convert html to pdf? If not like pdfkit andd wkhtmltopdf do, then any way to debug pdf generated by prawn for styling like we have way for wkhtmltopdf?


Solution

  • Could anyone tell, anyway we have in prawn gem to convert html to pdf?

    Prawn is not a html to pdf generator, they point this at official site http://prawnpdf.org/api-docs/2.0/

    "One thing Prawn is not, and will never be, is an HTML to PDF generator. For those needs, consider looking into FlyingSaucer via JRuby, or one of the webkit based tools, like Wicked or PDFKit. We do have basic support for inline styling but it is limited to a very small subset of functionality and is not suitable for rendering rich HTML documents."


    If not like pdfkit andd wkhtmltopdf do, then any way to debug pdf generated by prawn for styling like we have way for wkhtmltopdf?

    To debug Prawn generated pdf I think the easiest way is to generate a new pdf or make your controller respond with your pdf:

    Prawn::Document.generate("path/to/pdf/example.pdf") do
      pdf.text "Hello"
    end
    

    Controller respond the pdf:

    format.pdf do
      pdf = PdfGenerator.new(@invoice)
      send_data pdf.render, 
        filename: "pdf_test",
        type: 'application/pdf',
        disposition: 'inline'
    end
    

    And refresh your page every time that you make a change.

    To test your generated pdf, pdf-inspector gem is good.

    These tutorials may help you:

    https://grzegorowski.com/using-prawn-gem-for-generating-pdfs-in-rails-5/

    https://rubyplus.com/articles/3891-PDFs-with-Prawn-in-Rails-5