README.md in wicked_pdf-1.0.6 vs README.md in wicked_pdf-1.1.0

- old
+ new

@@ -2,11 +2,11 @@ ## A PDF generation plugin for Ruby on Rails Wicked PDF uses the shell utility [wkhtmltopdf](http://wkhtmltopdf.org) to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked PDF take care of the hard stuff. -_Wicked PDF has been verified to work on Ruby versions 1.8.7 through 2.1; Rails 2 through 4.1_ +_Wicked PDF has been verified to work on Ruby versions 1.8.7 through 2.3; Rails 2 through 5.0_ ### Installation Add this to your Gemfile and run `bundle install`: @@ -33,11 +33,11 @@ gem 'wkhtmltopdf-binary' ``` To your Gemfile and run `bundle install`. -This wrapper may trail in versions, at the moment it wraps the 0.9 version of `wkhtmltopdf` while there is 0.12 version available. Some of the advanced options listed below is not available with 0.9. +This wrapper may trail in versions, at the moment it wraps the 0.9 version of `wkhtmltopdf` while there is 0.12 version available. Some of the advanced options listed below are not available with 0.9. If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer: ```ruby WickedPdf.config = { @@ -60,11 +60,11 @@ end end ``` ### Usage Conditions - Important! -The wkhtmltopdf binary is run outside of your Rails application; therefore, your normal layouts will not work. If you plan to use any CSS, Javascript, or image files, you must modify your layout so that you provide an absolute reference to these files. The best option for Rails without the asset pipeline is to use the `wicked_pdf_stylesheet_link_tag`, `wicked_pdf_image_tag`, and `wicked_pdf_javascript_include_tag` helpers or to go straight to a CDN (Content Delivery Network) for popular libraries such as jQuery. +The wkhtmltopdf binary is run outside of your Rails application; therefore, your normal layouts will not work. If you plan to use any CSS, JavaScript, or image files, you must modify your layout so that you provide an absolute reference to these files. The best option for Rails without the asset pipeline is to use the `wicked_pdf_stylesheet_link_tag`, `wicked_pdf_image_tag`, and `wicked_pdf_javascript_include_tag` helpers or to go straight to a CDN (Content Delivery Network) for popular libraries such as jQuery. #### wicked_pdf helpers ```html <!doctype html> <html> @@ -81,10 +81,33 @@ <%= yield %> </div> </body> </html> ``` + +Using wicked_pdf_helpers with asset pipeline raises `Asset names passed to helpers should not include the "/assets/" prefix.` error. To work around this, you can use `wicked_pdf_asset_base64` with the normal Rails helpers, but be aware that this will base64 encode your content and inline it in the page. This is very quick for small assets, but large ones can take a long time. + +```html +<!doctype html> +<html> + <head> + <meta charset='utf-8' /> + <%= stylesheet_link_tag wicked_pdf_asset_base64("pdf") %> + <%= javascript_include_tag wicked_pdf_asset_base64("number_pages") %> + + </head> + <body onload='number_pages'> + <div id="header"> + <%= image_tag wicked_pdf_asset_base64('mysite.jpg') %> + </div> + <div id="content"> + <%= yield %> + </div> + </body> +</html> +``` + #### CDN reference In this case, you can use that standard Rails helpers and point to the current CDN for whichever framework you are using. For jQuery, it would look somethng like this, given the current versions at the time of this writing. ```html <!doctype html> @@ -106,13 +129,13 @@ respond_to do |format| format.html format.pdf do render pdf: 'file_name', disposition: 'attachment', # default 'inline' - template: 'things/show.pdf.erb', + template: 'things/show', file: "#{Rails.root}/files/foo.erb" - layout: 'pdf.html', # use 'pdf.html' for a pdf.html.erb file + layout: 'pdf', # for a pdf.html.erb file wkhtmltopdf: '/usr/local/bin/wkhtmltopdf', # path to binary show_as_html: params.key?('debug'), # allow debugging based on url param orientation: 'Landscape', # default Portrait page_size: 'A4, Letter, ...', # default A4 page_height: NUMBER, @@ -130,10 +153,11 @@ user_style_sheet: 'URL', cookie: ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format post: ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format redirect_delay: NUMBER, javascript_delay: NUMBER, + window_status: 'TEXT', # wait to render until some JS sets window.status to the given string image_quality: NUMBER, no_pdf_compression: true, zoom: FLOAT, page_offset: NUMBER, book: true, @@ -155,24 +179,24 @@ outline_depth: LEVEL }, margin: { top: SIZE, # default 10 (mm) bottom: SIZE, left: SIZE, right: SIZE }, - header: { html: { template: 'users/header.pdf.erb', # use :template OR :url - layout: 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout + header: { html: { template: 'users/header', # use :template OR :url + layout: 'pdf_plain', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout url: 'www.example.com', locals: { foo: @bar }}, center: 'TEXT', font_name: 'NAME', font_size: SIZE, left: 'TEXT', right: 'TEXT', spacing: REAL, line: true, content: 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string) - footer: { html: { template:'shared/footer.pdf.erb', # use :template OR :url - layout: 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout + footer: { html: { template:'shared/footer', # use :template OR :url + layout: 'pdf_plain.html', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout url: 'www.example.com', locals: { foo: @bar }}, center: 'TEXT', font_name: 'NAME', font_size: SIZE, @@ -212,10 +236,23 @@ end end ``` By default, it will render without a layout (layout: false) and the template for the current controller and action. +#### wkhtmltopdf Binary Options + +Some of the options above are being passed to `wkhtmltopdf` binary. They can be used to control the options used in Webkit rendering before generating the PDF. + +Examples of those options are: + +```ruby +print_media_type: true # Passes `--print-media-type` +no_background: true # Passes `--no-background` +``` + +You can see the complete list of options under "Global Options" in wkhtmltopdf usage [docs](http://wkhtmltopdf.org/usage/wkhtmltopdf.txt). + ### Super Advanced Usage ### If you need to just create a pdf and not display it: ```ruby # create a pdf from a string @@ -228,18 +265,18 @@ # create a pdf from a URL pdf = WickedPdf.new.pdf_from_url('https://github.com/mileszs/wicked_pdf') # create a pdf from string using templates, layouts and content option for header or footer pdf = WickedPdf.new.pdf_from_string( - render_to_string('templates/pdf.html.erb', layout: 'pdfs/layout_pdf'), + render_to_string('templates/pdf', layout: 'pdfs/layout_pdf'), footer: { content: render_to_string(layout: 'pdfs/layout_pdf') } ) # or from your controller, using views & templates and all wicked_pdf options as normal -pdf = render_to_string pdf: "some_file_name", template: "templates/pdf.html.erb", encoding: "UTF-8" +pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8" # then save to a file save_path = Rails.root.join('pdfs','filename.pdf') File.open(save_path, 'wb') do |file| file << pdf @@ -247,10 +284,22 @@ ``` If you need to display utf encoded characters, add this to your pdf views or layouts: ```html <meta charset="utf-8" /> ``` + +### Page Breaks + +You can control page breaks with CSS. + +Add a few styles like this to your stylesheet or page: +```css +div.alwaysbreak { page-break-before: always; } +div.nobreak:before { clear:both; } +div.nobreak { page-break-inside: avoid; } +``` + ### Page Numbering A bit of javascript can help you number your pages. Create a template or header/footer file with this: ```html <html> @@ -299,12 +348,16 @@ ``` If you use the standard `render pdf: 'some_pdf'` in your app, you will want to exclude those actions from the middleware. ### Further Reading +Mike Ackerman's post [How To Create PDFs in Rails](https://www.viget.com/articles/how-to-create-pdfs-in-rails) + Andreas Happe's post [Generating PDFs from Ruby on Rails](http://www.snikt.net/blog/2012/04/26/wicked-pdf/) JESii's post [WickedPDF, wkhtmltopdf, and Heroku...a tricky combination](http://www.nubyrubyrailstales.com/2013/06/wickedpdf-wkhtmltopdf-and-herokua.html) + +Berislav Babic's post [Send PDF attachments from Rails with WickedPdf and ActionMailer](http://berislavbabic.com/send-pdf-attachments-from-rails-with-wickedpdf-and-actionmailer/) StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf) ### Debugging