Creating a page break when creating a PDF using wicked_pdf

sample.erb.html

<p>Page 1</p1> <p>Page 2</p2> 

So, everything after "Page 1" I want to print on page 2.

How can i do this?

There is one solution in https://stackoverflow.com/a/3/198861/ ... , but this did not work for me.

For example, in the case of shrimp, it has a nice function called start_new_page

+4
source share
3 answers

in your css

 p{page-break-after: always;} 

Update

After a few questions, I will expand my answer and use it in my applications.

1 Several times, wickedpdf helpers do not work, so I add an initializer

_config / Initializers / wiked_pdf.rb _

 module WickedPdfHelper def wicked_pdf_stylesheet_link_tag(*sources) sources.collect { |source| "<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css")}</style>" }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe end def wicked_pdf_image_tag(img, options={}) image_tag wicked_pdf_image_location(img), options end def wicked_pdf_image_location(img) "file://#{Rails.root.join('app', 'assets', 'images', img)}" end def wicked_pdf_javascript_src_tag(source) "<script type='text/javascript'>#{Rails.application.assets.find_asset("#{source}.js").body}</script>" end def wicked_pdf_javascript_include_tag(*sources) sources.collect{ |source| wicked_pdf_javascript_src_tag(source) }.join("\n").html_safe end WickedPdf.config = { } end 

2 In the application controller, create a configuration method with general configuration parameters

_app / controllers / application_controller.rb _

 class ApplicationController < ActionController::Base def pdf_config WickedPdf.config = { :wkhtmltopdf => "/usr/local/bin/wkhtmltopdf", :orientation => 'Landscape', :layout => "pdf.html", :footer => { :left => "Rectores Lideres Transformadores", #:left => "#{Entidad.find(@current_user.entidad).nombre}", :right => "#{Time.now}", :font_size => 5, :center => '[page] de [topage]' }, :disposition => 'attachment' } end end 

3 Create a common layout for all of your PDF files. Here I use my css application in order to maintain the same appearance of the web page in pdf reports, only I have to use the same classes and id

Application / Layouts / pdf.html.erb

 <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <%= wicked_pdf_stylesheet_link_tag "application" %> ----- HERE YOUR APPLICATION CSS ----- </head> <div id="content"> <%= yield %> </div> </body> </html> 

4 Add PDF Redirection to Your Controllers

_app / controllers / users_controller.rb _

  def index @users = User.all respond_to do |format| format.pdf do pdf_config render :pdf => "filename" end end end 

5 Now, in your css, select which html id is the page brake

  #brake{page-break-after: always;} 
+3
source

I had the same problem and I discovered something that might help. This was my page break CSS:

 .page-break { display: block; clear: both; page-break-after: always; } 

This did not work due to TWO strong> reasons :

I. In one of the imported SASS files, I had this line of code:

 html, body overflow-x: hidden !important 

II. Another issue was bootstrap

 @import "bootstrap" 

It looks like from float: left to:

 .col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 { float: left; } 

page break no longer works. So just add this after which you are importing bootstrap.

 .col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 { float: initial !important; } 
+1
source

For everyone who has this problem, but none of these answers, like for me, just works: I refused to use CSS to fix page breaks and instead generated 2 pdf files and merged them together and used the resulting file. therefore, it is not possible that a page break does not exist.

To combine files , an array of PDF file names, I used

 system("pdftk #{files.join(' ')} cat output merged_file_name.pdf") 
0
source

Source: https://habr.com/ru/post/1494892/


All Articles