Whatever ones feelings on pdf, many people still find security in it as a replacement for paper. Pdf is widely used to store electronic copies of physical documents and transmit electronic flyers and brochures. Many people still print documents instead of keeping them on screen. Often, the file being printed will be in pdf. Pdf files continue to be relevant.
What sorts of things do we want to do with pdf? Here are some ideas:
1. Create reports
Prawn does the job. It is possible to stay DRY by collecting common elements to your reports such as a header, footer, divider or currency display helper in one file stored as a class e.g. ReportElements. You can then instantiate a new object taking from this class and use the methods contained within. If you need to change your header, then you only need to change one file.
2. Replace html views with a pdf option
Wicked PDF does this. If you want a higher level of control, then Prawn is a better option for generating reports. This way you can leave web pages to be more interactive and reports to be static. A good use case for Wicked PDF is a website' terms of service and other legal documentation. It's important to have only one file where the terms of service are updated to avoid spotting the difference. Wicked PDF parses the html and dynamically generates a pdf based on it.
3. Concatenate pdfs
CombinePDF seems to be a good option.
Putting it all together in a controller you could do something like this:
What sorts of things do we want to do with pdf? Here are some ideas:
- create reports
- replace html views with a pdf option (for ease of printing)
- concatenate pdfs
1. Create reports
Prawn does the job. It is possible to stay DRY by collecting common elements to your reports such as a header, footer, divider or currency display helper in one file stored as a class e.g. ReportElements. You can then instantiate a new object taking from this class and use the methods contained within. If you need to change your header, then you only need to change one file.
2. Replace html views with a pdf option
Wicked PDF does this. If you want a higher level of control, then Prawn is a better option for generating reports. This way you can leave web pages to be more interactive and reports to be static. A good use case for Wicked PDF is a website' terms of service and other legal documentation. It's important to have only one file where the terms of service are updated to avoid spotting the difference. Wicked PDF parses the html and dynamically generates a pdf based on it.
3. Concatenate pdfs
CombinePDF seems to be a good option.
Putting it all together in a controller you could do something like this:
def complicated_pdf
@model = Model.find params[:model_id]
respond_to do |format|
format.pdf do
# get Prawn document
pdf_1_data = ExamplePrawnPdf.new(@model, current_user, view_context).render
pdf_1 = CombinePDF.parse(pdf_1_data)
# get the terms of service through wkhtmltopdf shell utility and WickedPdf gem
pdf_2_data = render_to_string pdf: "", file: "#{Rails.root}/app/views/home/terms_of_service.erb", encoding: "UTF-8"
pdf_2 = CombinePDF.parse(pdf_2_data)
combined_pdf = CombinePDF.new
combined_pdf << pdf_1
combined_pdf << pdf_2
send_data combined_pdf.to_pdf,
filename: "Complicated_report_#{@model.some_unique_identifier}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
No comments:
Post a Comment