Re: Termal printers with fixed width and "infinite" height

3,056 views
Skip to first unread message

BrianS

unread,
Jun 14, 2013, 12:41:09 AM6/14/13
to dom...@googlegroups.com
Hm ... this is a hard problem. I've been trying to figure out a way you could do this and so far the best I've come up with is parsing the document twice: once to get a sense of the size of the document, and the second time to generate the output. So you start by parsing the document using a standard page size. Your paper is 8cm wide, so you'll want to define a custom paper, maybe 8cm x 20cm. You want the page size to be just large enough that you don't run into any bugs around paging. Set the page margins to 0 at top and bottom for this run. After you have parsed the document determine the number of pages and then multiply that by the page height you chose to get the height for the final render. After all this is done, perform a second render using your calculated page height.

I've never generated an overly tall page via dompdf, so I'm curious to know how well it works.


On Friday, June 7, 2013 1:26:07 AM UTC-4, Roberto Spadim wrote:
hi guys, i'm a developer of nfephp: http://www.nfephp.org/
and we generate "tax documents"
i will use HTML to PDF converter to allow a rapid development
but i have a problem...
the document may have different page sizes
1) A4 format (default)
2) a termal printer output with 8cm X 40meters

my problem is the document with page size (2)
since the HTML will get bigger and bigger, the size of page (height) will be one page only, and the width will be fixed (8cm)

how could i do this?

the HTML file is here:
-----------------------------
<STYLE>
td {text-align:center; font-size:10px;}
</STYLE>
<table>
<tr>
<td style='border-top:1px dotted black' colspan='3'>
{EMITENTE_RAZAO}<br>
{EMITENTE_ENDERECO}<br>
{EMITENTE_CIDADE}, {EMITENTE_UF}<br>
CNPJ: {EMITENTE_CNPJ} - IE: {EMITENTE_IE}</td>
</tr>
<tr>
<td>No.: {NF_NUMERO}</td>
<td>Série: {NF_SERIE}</td>
<td>{NF_EMISSAO_DD_MM_YYYY}</td>
</tr>
<tr>
<td style='border-top:1px dotted black' colspan='3'>
DANFE NFC-E - Nota Fiscal Eletrônica para Consumidor Final<br>
NFC-e não permite aproveitamento de crédito de ICMS
{AMBIENTE_DE_TESTE}</td>
</tr>
<tr>
<td style='border-top:1px dotted black' colspan='3'>
Consulte pela Chave de Acesso em {WS_SITE}<br>
<b>CHAVE DE ACESSO</b><br>
{CHAVE DE ACESSO}<br>
<img src="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAAyAQMAAADcGHRpAAAABlBMVEX///8AAABVwtN+AAAAQUlEQVQ4jWNgGAWDAUgVnXPt02013zqnWyaaKz/bUsxim8i9a0U6hTmae9c8iTgR8XpU2aiyUWWjygaXslFAQwAA3QLlKoW/9MoAAAAASUVORK5CYII="></td>
</tr>
<tr>
<td style='border-top:1px dotted black' colspan='3'>
Consulta via leitor de QR-CODE<br>
<img src="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQMAAACXljzdAAAABlBMVEX///8AAABVwtN+AAAEn0lEQVRYhe2YsYrjOhSGjxGMmmC3LgR6hUmnacavEtgXkElrYhm3i/0CgXkVTxN3ySsIVKS1mUYLwuceJ3v3VpO9o23XVZwPYusc/f9/FIC/F0CBIYhN0nTPfODgLGuNrtFHEgVCCEjTej4pZZmUFlJN30YSFLY2KZQ4FCNKxLbtyuUPSGDfy0UnnhdcOof99D/IelPPQ4EoLWNBp39AQDCzb+bmcgKfW7A1FQ5iCXXBHfdd2W1PasydWG9+9efLhC7B2knD9gQjBsvsfvm1ST4hhReh0lTqc4EDoBMs1M1tpVFk/Umtk34psOAMqHRzM8USuGJgRmt4TnAExyywSacQSTi30jDsSnz3yAXCLuv72T8kVGtbbbIuO78MY07vZgDKBSKJyi1767t1ayvIIcijrk3mI0lhpZDtBCk88SJ3TL6ZPyHcsbY3aTZwVNbh2pLsfXhIFEnTTaVJi1OhciZpj036WUUS7i3Dqe5gO8Bwpc+2Kufbc2KI8sKCzno8PwGS1zE09ZLEEjIU1rbNckhfRn4NADuqwU1znxPAgDYz5ZIWA+SkErHHe61jCJfM2eqQpikfqIsgXT9321hSQGBtKA0UC/U3CGaz+V63zwmphKTVNHSnSCXSWV3OE0QSJaXDj371axilYPJIdcchktDSSPUUPwMhT1kkNnVz8bHES/dmKBuTETmuXahJMuohKZCEvknhkK4qocRocYLb3okiMtxMURcn5TkgiGyettEkt/K4ORz09sfguRDoTIK3LkQRkkaVLbq+jJSNQJb0PcPlN4TehzaSSUhZniNZ2nRILkMkUZLSOSTzAk/KQ7ACqrQ8q0hCXQi7qqRJ6AkG7qxs+y6ZYgnnVKm6q5t5xMGyIGD1E3hIAJnYsQ7Ky4kXVxqemMlM4mMJuVi1n+cpeaeyCZAUH/AKkYQSWVS0qcrhVZGtCXaEzBQ+llgkd9EJBW3hESxUNc73LnxKaMBwbTiQSoAP1gYX9kt9c6QYsj7H1jPlD93mIKH+nmZzLOGeVrpHyp/3kd5VoNWp3sYSdUWKHLJrUFzlwrqwqU3ymHAOpPu0bs6K5GcF4keDFx9LPJKpTklzflX+Sm2og67PKpIU1oKoIMHlabVbCd8+uvLdPyY5ecgmWQ5rrS3l4Y7195XGEAVOmn3fmGQcC3DB0SCjtz6S8Jx8Y3/Ln4LTDI1vBtagjiPFlbkjrPnzgpRmUtDQuNxnsc8Jl3jcm2SBFz/kCDR4z3h3pAiyzl8O0RyeXwYvA6lEH8p4Qg7b9hSHxY9B0bBhSfR3lcQQSmex20+ZIZX4dYoATUv5DfFByA+kWi+crwlIo/d9Uo0hKgch6SRRTq8F8uDkW6iXnzP51wm/UoSG+5nJW7IkNtV4108EWR3uWK1nJppUr4ya+kGD6mNC5xKxq/QhfX1VA3XRsn4pfyb618ntxM9mU+IJBhAWW5Mm8QTpDLlJU02TEFoS4H8zXxyhyZ2OcGeae70lsyOrWeB3xOE64iQ/6MzPBHwjB/j3H4yvE1od+5ixv5wUt3RGN1XZ/XSkrxPqAr4h5c92uAfGjl56G0v+Xp9f/wAB7INipCG5ogAAAABJRU5ErkJggg=="><br>
Protocolo de Autorização: {NFE_PROTOCOLO}<td>
</tr>
<tr>
<td style='border-top:1px dotted black' colspan='3'>
CONSUMIDOR<br>
CPF/CNPJ/ID Estrang.: {CONSUMIDOR_DOCUMENTO}</td>
</tr>
<tr>
<td style='border-top:1px dotted black' colspan='2'>Quant Total de Ítens</td>
<td style='border-top:1px dotted black' align='right'>{TOTAL_ITENS}</td>
</tr>
<tr>
<td colspan='2'>VALOR TOTAL {MOEDA}</td>
<td align='right'>{TOTAL_VALOR_NF}</td>
</tr>
<tr>
<td colspan='2'>VALOR DESCONTOS {MOEDA}</td>
<td align='right'>{TOTAL_VALOR_DESCONTO}</td>
</tr>
<tr>
<td colspan='2'>FORMA PAGAMENTO</td>
<td align='right'>{FORMA_PAGAMENTO}</td>
</tr>
<tr>
<td colspan='2'>VALOR PAGO {MOEDA}</td>
<td align='right'>{TOTAL_VALOR_PAGO}</td>
</tr>
<tr>
<td style='border-bottom:1px dotted black;border-top:1px dotted black' colspan='2'>Valor Aproximado dos Tributos {MOEDA}</td>
<td style='border-bottom:1px dotted black;border-top:1px dotted black' align='right'>{TOTAL_VTOTTRIB}</td>
</tr>
</table>
-----------------------------

Roberto Spadim

unread,
Jun 14, 2013, 7:08:57 PM6/14/13
to dom...@googlegroups.com
well i done the work with mpdf
i done a first render to get height and a second with that height
but... well a lot of time, maybe some optimization coudl be done

i couldn't do with dompdf, any help is wellcome

another point... i have many documents using fpdf, could i use fpdf + dompdf? i need generate pages in fpdf, some others in mpdf or dompdf and after merge document of fdpf with document of dompdf/mpdf

Riccardo Fraioli

unread,
Jun 15, 2013, 3:54:57 AM6/15/13
to dom...@googlegroups.com
I did not understand you need to set height automatically?
If Yes -> I had solved with a small PHP script before making HTML output
that was changing measurements HEIGHT!
If you can not use php uses another language.
If - NO! Pardon
Hello


2013/6/15 Roberto Spadim <rob...@spadim.com.br>

--
You received this message because you are subscribed to the Google Groups "dompdf" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dompdf+un...@googlegroups.com.
To post to this group, send email to dom...@googlegroups.com.
Visit this group at http://groups.google.com/group/dompdf.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

BrianS

unread,
Jun 15, 2013, 8:44:47 AM6/15/13
to dom...@googlegroups.com, rifr...@gmail.com
Yes, you have to set the height yourself. dompdf is not currently able to automatically determine the maximum height necessary to contain a document.

BrianS

unread,
Jun 15, 2013, 8:51:45 AM6/15/13
to dom...@googlegroups.com
Technically, it should be possible to use fpdf as the back end rendering engine of dompdf. However, we do not have an adapter that would allow it. If someone were to write an adapter that conforms to the Canvas interface object then you would be able to use that with dompdf. We have bigger issues to address right now so I doubt someone on the dompdf team would be up for writing the adapter. We're happy to give advice on the process if anyone is interested in giving it a shot.

I'll try to work up some sample code for using dompdf to get an infinite-height pdf. Sounds like it should be similar to what you did with mPDF.

Roberto Spadim

unread,
Jun 16, 2013, 5:19:42 PM6/16/13
to dom...@googlegroups.com, rifr...@gmail.com
Em sábado, 15 de junho de 2013 04h54min57s UTC-3, Riccardo Fraioli escreveu:
I did not understand you need to set height automatically?
If Yes -> I had solved with a small PHP script before making HTML output
that was changing measurements HEIGHT!
any example?
 
If you can not use php uses another language.
If - NO! Pardon
yes php language only
 
Hello

Riccardo Fraioli

unread,
Jun 17, 2013, 2:46:29 AM6/17/13
to dom...@googlegroups.com
I do not remember very well.
but I tried to experiment with the calculation of lines per
height that I wanted to occupy and / or the edge of the bottom and / or the font-size.

hinit = 10;
$ nCount = something;
heightfinal nCount = $ x hinit;

or in some thermal printer or in the settings of the driver,
you might see
if there is the option continuous roll ...
hello

BrianS

unread,
Jun 19, 2013, 1:05:21 AM6/19/13
to dom...@googlegroups.com
OK, so I had a chance to try this out and it should work. Follows is an explanation of how you could do this using dompdf.

First, you need to make a minor modification to the stylesheet of your document. You want to set the top and bottom page margins to zero so they don't add in to the height calculation. So in your first-pass HTML add the following additional style declaration:

@page { margin-top: 0px; margin-bottom: 0px; }

Next, you have to determine what the best first-pass document size will be. I was using a simple document, so I went with a simple 8cm x 8cm page size. You'll probably want to use a larger height to avoid any bugs related to paging. 8cm is roughly 226.77pt. So I set up the first pass to use a document defined with the appropriate dimensions:

$dompdf = new DOMPDF( );
$dompdf->set_paper( array( 0 , 0 , 226.77 , 226.77 ) );
$dompdf->load_html( $first_pass_html );
$dompdf->render( );


Then we get the number of pages that resulted from this pass and unset the $dompdf variable so we can do our second pass:

$page_count = $dompdf->get_canvas( )->get_page_number( );
unset( $dompdf );


Finally, render the document a second time using a page height calculated from the page height used in the first pass multiplied by the number of pages generated (plus a little extra padding to accommodate margins).

$dompdf = new DOMPDF( );
$dompdf->set_paper( array( 0 , 0 , 226.77 , 226.77 * $page_
count + 20 ) );
$dompdf->load_html( $original_html );
$dompdf->render( );
$dompdf->stream( 'sample.pdf' , array( 'Attachment' => 0 ) );

That should do it.
-b

Roberto Spadim

unread,
Jun 19, 2013, 5:16:01 PM6/19/13
to dom...@googlegroups.com
what about the white space after last text? can remove it?

Riccardo Fraioli

unread,
Jun 19, 2013, 5:19:12 PM6/19/13
to dom...@googlegroups.com
roberto did you drink?

better explains the request...


2013/6/19 Roberto Spadim <rob...@spadim.com.br>
what about the white space after last text? can remove it?

--

BrianS

unread,
Jun 20, 2013, 3:32:53 PM6/20/13
to dom...@googlegroups.com
On Wednesday, June 19, 2013 5:16:01 PM UTC-4, Roberto Spadim wrote:
what about the white space after last text? can remove it?

Not using this method, so a little extra would be fed out at the end of any document printed. dompdf does keep track of the position where the next object should be rendered, but I'm not sure there's any way to get that information. If I have time I'll look into it further.

Roberto Spadim

unread,
Jun 20, 2013, 4:59:28 PM6/20/13
to dom...@googlegroups.com, rifr...@gmail.com
i accept a beer :) hehe
i did what you told me, created the doc with a4 size, count the pages and recreate with pages * a4 size + 10
it did the work, but after the last word of my html doc, i got a very big white space (i think it's because the a4 size is bigger than the real size without pages)
Reply all
Reply to author
Forward
0 new messages