如何使用 DOMPDF 和 Laravel 使 pdf 精确 10CM 宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40843050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:49:54  来源:igfitidea点击:

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

phplaravelpdfdompdf

提问by Rubberduck1337106092

I am creating a PDF with DOMPDFand laravel.

我正在用DOMPDF和 laravel创建一个 PDF 。

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

pdf是用专门的打印机打印的,只接受10CM宽20CM高的文件。

I have tried this:

我试过这个:

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

Since 11X17 is

由于 11X17 是

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

I figured 10X20 was 0,0720,1440

我认为 10X20 是 0,0720,1440

But it's not working.

但它不起作用。

Any help is appreciated.

任何帮助表示赞赏。

Thanks in advance

提前致谢

回答by Rubberduck1337106092

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.

下载在 Acrobat Reader 中打开的 PDF 将鼠标移到左角,现在您可以看到文档的宽度和高度,我相应地更改了自定义纸张。

The end result was: 10CM X 20CM =

最终结果是: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

谢谢

回答by BrianS

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

在 PHP 中设置纸张大小需要知道点 (pt) 度量,点是原生 PDF 单位。PDF 分辨率(使用 Dompdf)为 72pt/inch。所以 10cm x 20cm 大致相当于 283 X 566(如您在回答中所述)。

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.

但是,您可以通过在 CSS 中指定页面尺寸来允许 Dompdf 计算适当的磅值。这从 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.

琐事:PDF 规范没有提供指示纸张方向的方法(尽管有指示旋转的方法)。Dompdf 只是在指定横向时翻转宽度/高度。

回答by Rahul

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

我认为问题在于方向,因为 setPaper默认使用“A4”方向,所以这可能是您的代码不起作用的原因。

/**
     * 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.

希望有所帮助或尝试其他选项,而不是portrait.