Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pdf exact 10CM width with DOMPDF and Laravel?

I am creating a PDF with DOMPDF and laravel.

The pdf is being printed with a special printer that only accepts files with 10CM width and 20CM height.

I have tried this:

$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper);

Since 11X17 is

"11x17" => array(0, 0, 792.00, 1224.00),

I figured 10X20 was 0,0720,1440

But it's not working.

Any help is appreciated.

Thanks in advance

like image 283
Rubberduck1337106092 Avatar asked Nov 28 '16 11:11

Rubberduck1337106092


2 Answers

How i fixed this:

change the custom paper.

Download the PDF open in Acrobat Reader move your mouse to the left corner now you can see the width and height of the document, and i changed the custom paper accordingly.

The end result was: 10CM X 20CM =

$customPaper = array(0,0,567.00,283.80);
$pdf = PDF::loadView('pdf.retourlabel', compact('retour','barcode'))->setPaper($customPaper, 'landscape');

Very circuitous work but i did get the Job done..

Thanks

like image 138
Rubberduck1337106092 Avatar answered Oct 14 '22 15:10

Rubberduck1337106092


Setting the paper size in PHP requires knowing the point (pt) measurement, points being the native PDF unit. The PDF resolution (with Dompdf) is 72pt/inch. So 10cm x 20cm is roughly equivalent to 283 X 566 (as you noted in your answer).

1 inch = 72 point
1 inch = 2.54 cm
10 cm = 10/2.54*72 = 283.464566929
20 cm = 10/2.54*72 = 566.929133858

landscape is mean opposite. So, we can set it like : array(0, 0, 566.929133858, 283.464566929 ) which same as the answer but in more precise value

You can, however, allow Dompdf to calculate the appropriate point size by specifying your page dimensions in CSS. This is available starting with Dompdf 0.6.2.

<html>
<head>
  <style>
    @page { size: 10cm 20cm landscape; }
  </style>
</head>
<body>
  ...
</body>
</html>

Trivia: the PDF spec does not provide for a method of indicating paper orientation (though there is a method of indicating a rotation). Dompdf just flips the width/height when landscape orientation is specified.

like image 36
BrianS Avatar answered Oct 14 '22 16:10

BrianS