简体   繁体   中英

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

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

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 ).

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.

I think the issue is with orientation, since the setPaper uses 'A4' orientation as default so this might be the reason that your code is not working.

/**
     * Set the paper size (default A4)
     *
     * @param string $paper
     * @param string $orientation
     * @return $this
     */
    public function setPaper($paper, $orientation = 'portrait'){
        $this->paper = $paper;
        $this->orientation = $orientation;
        $this->dompdf->setPaper($paper, $orientation);
        return $this;
    }

Try using :

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

Hope that helps or try other options too instead of portrait .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM