0

I am trying to make an A4 page with mPDF with 20 lines. It should always fit on one page with the same font size and line height. The text in each line can vary in length. If it's too long, I just want to strip it at the end. I tried with white-space:nowrap with no luck, the text keeps going in a new line. Tried with tables, divs, multiple elements.

My mpdf configuration:

$mpdf = new Mpdf([
  'mode' => 'utf-8',
  'format' => 'A4',
  'margin_left' => 0,
  'margin_right' => 0,
  'margin_top' => 0,
  'margin_bottom' => 0,
  'margin_header' => 0,
  'margin_footer' => 0
  ]);

My template:

<style>
    table {
        padding: 0 3%;
        margin: 0;
        overflow: hidden;
        width: 100%;
        white-space: nowrap;
    }
    table td {
        width: 100%;
        white-space: nowrap;
        font-size: 36px;
        line-height: 46px;
    }
</style>

<table autosize="1">
    {% for i in 1..20 %}
        <tr>
            <td>
                {{i}}.  Text of varying length (if it's short the rows are ok, but not if it's long)
            </td>
        </tr>
    {% endfor %}
</table>

Would appreciate any help of suggestions what I may be doing wrong. Thank you!

8
  • In a case like this it is important to use a monospace font. Get the maximum number of characters that can fit per line. Then, in Volt, add a conditional to limit the number of characters per line.
    – Arthur
    Aug 27, 2022 at 10:52
  • Did you find a solution for this? I have dynamic text length, and dynamic number of rows, so limiting the number of characters won't work. Apr 25 at 5:38
  • I had a similar project, we needed pixel precision because the result was going to be printed in pre-printed forms: always, before inserting the line, the length of the string was controlled: in our case, over 45 characters it was truncated; the monospace font was essential for achieving this.
    – Arthur
    Apr 25 at 12:00
  • This seems like a less than ideal solution :/ Jul 21 at 5:47
  • Proven and works. If you find a better one, please share it.
    – Arthur
    Jul 21 at 7:49

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.