Wicked_pdf: Page Break not working.

Created on 10 Apr 2015  路  11Comments  路  Source: mileszs/wicked_pdf

css

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

html

Tried using in style and using other html element tags too.

Also there is anyway to know how many pages there gonna be??
So I can dynamically set a page break to make the last page not so clean. I got only one line of text in the last one, so I would like to mate the last paragraph go to the last page if the number of pages is equals to 2.

Most helpful comment

This is the css I use to control page-breaking:

div.alwaysbreak { page-break-before: always; }
div.nobreak:before { clear:both; }
div.nobreak{ page-break-inside: avoid; }

These classes won't work on certain types of elements, but they always seem to work for divs, so wrap your paragraphs with <div class="nobreak">, and if it can fit on one page, it will, but if the last line of a paragraph crosses the page border, the entire paragraph will be shifted to page2.

Other than that, you could calculate the height of the element with javascript and see if it is tall enough to cross the page boundary, and if so, resize the text or adjust the line-height or something, but that seems very fiddly and error prone.

All 11 comments

This is the css I use to control page-breaking:

div.alwaysbreak { page-break-before: always; }
div.nobreak:before { clear:both; }
div.nobreak{ page-break-inside: avoid; }

These classes won't work on certain types of elements, but they always seem to work for divs, so wrap your paragraphs with <div class="nobreak">, and if it can fit on one page, it will, but if the last line of a paragraph crosses the page border, the entire paragraph will be shifted to page2.

Other than that, you could calculate the height of the element with javascript and see if it is tall enough to cross the page boundary, and if so, resize the text or adjust the line-height or something, but that seems very fiddly and error prone.

used < div style="clear:both;page-break-inside: avoid;"> paragraph < /div> and still breaks the end in the next page.

I installed the wkhtmltopdf throw the gem (gem 'wkhtmltopdf-binary', '0.9.9.3')

Please try separating it into a stylesheet or a <style> block. Note that nobreak has a pseudo :before class on it.

Same result with the css in a stylesheet and the div with the class="nobreak"

Could you provide the HTML that you are creating?

and how can I get the HTML generated?
I use this comando to create the pdf

render :pdf => "Contrato #{@aluno.nome} #{Date.today}", :footer => { :right => '[page] de [topage]' }, :template => 'pdf_generator/contrato.html.erb'

if I call in the browser without the .pdf it will give me the html but with another layout wrapper, javascript and css.

Add the option :show_as_html => true:

render :pdf => "Contrato #{@aluno.nome} #{Date.today}",
  :footer => { :right => '[page] de [topage]' },
  :template => 'pdf_generator/contrato.html.erb',
  :save_as_html => true

Then on the resulting page, right-click->view source

I got the problem. I was including the application css file, get from someone googling how to do it, and some css from my applications was broken the nobreak, including just the css fo the pdf file worked fine. Thanks for the help man. Appreciate.

God bless show_as_html option =D

Glad to help!

Hi!

Using the option page-break, is generated one blank page after the last data page. How to fix this?

`<%= card.user.name %>

<%= wicked_pdf_image_tag_for_public( barcode ) %>

<%= card.card_number %>`

@lucianobezerra That isn't enough information for me to go on or reproduce.

Where are page-break CSS rules applied?
Is there a loop where you are outputting a page break at the end of the loop? (maybe you can move it to the top of the loop if the index is greater than 0)
Is is possible your content has bottom padding or margin causing it to flow to the next page?
Is the PDF page margin too big to contain your content causing a flow to the next page?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lisamburns picture lisamburns  路  3Comments

andheiberg picture andheiberg  路  3Comments

gregblass picture gregblass  路  4Comments

m-andreas picture m-andreas  路  5Comments

douglasep picture douglasep  路  4Comments