Skip to content
Advertisement

mPDF – Page break based on element height

I’m using mpdf in Laravel and with this package version 4.0

The problem is that I have a list of questions in different heights which have 4 options, and I don’t know the exact and approximate height of every div tag which question and answer options are there, I don’t want some part of my question to go to another page

All parts of my question have to be on one page and if this couldn’t happen, mpdf put that question tag element on the next page (all parts of questions)

this picture is for now which is wrong:

sample question lists

what I want:

This image

so for this problem, I want to know the height of my question element so I can determine if the new page is needed to add a page break(same as this question on StackOverflow), or any other solution to fix my issue

this is my blade file:

JavaScript

and I tried all methods in the documentation of mpdf and this answer but results were not useful

if any other solution exists please guide me

Advertisement

Answer

After lots of tries, the issue solved!

the page-break-inside: avoid; doesn’t work when I have a loop of block elements

I used a table for every question and put it in the main table and in it’s <td> tag, the inner table now has autosize="1" which was needed

also, add this config to mPdf

JavaScript

I added page-break-inside: avoid to the main table and the <tr> of questions in the loop

note that if you faced undefined index error try to solve it generally! but if like me, it doesn’t fixed use error_reporting(0);

the corrected blade file:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement