6
I currently use the mpdf library for reporting. However, in some reports I need to send more than 400 pdf pages and this ends up taking a long time and consuming many server resources.
I even checked questions related to library performance to try to optimize it, but nothing helped mpdf Slow!
So I decided to create my reports in HTML and CSS, and found some difficulties:
1° I will be dependent on resources (plugins and related) of the browser for printing in pdf.
2° My layout remains well standardized being a header on all pages and a table with information. So I need to know when the information fills the entire A4 sheet to create a new page and repeat the process.
I used Google Chrome’s native PDF save and Cutepdf Writer to send in pdf, but they have differences in display form so my question is:
How to recognize the page boundary?
How to improve compatibility in printing them?
Example of the procedure.
body {
background: rgb(204, 204, 204);
}
page {
background: white;
display: block;
margin: 0 auto;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);
}
page[size="A4"] {
width: 21cm;
height: 29.7cm;
}
page[size="A4"][layout="portrait"] {
width: 29.7cm;
height: 21cm;
}
@media print {
body,
page {
margin: 0;
box-shadow: 0;
}
}
.header {
padding-top: 10px;
text-align: center;
border: 2px solid #ddd;
}
table {
border-collapse: collapse;
width: 100%;
font-size: 80%;
}
table th {
background-color: #4caf50;
color: white;
text-align: center;
}
th,
td {
border: 1px solid #ddd;
text-align: left;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
<page size="A4">
<div class="header">
[nomeEmpresa]
<br>[endereco] - [cidade] - [cep]
<br>[cnpj] - [telefone]
<br>
<h3>[nomeRelatorio] - [tipoRelatorio]</h3>
</div>
<table class="table">
<thead>
<tr>
<th>[coluna0]</th>
<th>[coluna1]</th>
<th>[coluna2]</th>
<th>[coluna3]</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</page>
<page size="A4"></page>
also in the: jsfiddle
What specific difficulty are you having? Supposedly, working with exact measurements, it would only be the case for your application to calculate the division of texts before generating HTML. For free text, it needs a certain ingenuity, which would probably be a bit complex to solve in one question. (in fact this type of problem, in an ideal world, would not be solved with web application, which is naturally inferior to a desktop, but this escapes the scope of the question :P )
– Bacco
A possible solution is you see the type of font you will use and "choose" a predetermined number of lines per page. If you use a monospace, the space that the text occupies is perfectly calculable, sacrificing a little of the aesthetic in favor of the predictability of the output. Then the breaks would be managed in the server-side application. In sources not proportional you can do, but you have to give a reasonable margin of error, and have the measurements of the characters on the server for calculation purposes. It’s a bit more work. CSS in theory would have predicted some features, but only "work in theory":) even.
– Bacco
@Bacco thought to do this way but some texts presented larger and passed the size, the monospace font could solve this problem?
– Gabriel Rodrigues
Only with CSS not. It would be the case to use monospace, and count the characters in the server-side application. When you reach a certain amount, close the page and open the next one. If all the lines are shorter than the width is even simpler, just count per line. But if you’re going to have paragraphs, then you need to elaborate better.
– Bacco