如何使用 DOMPDF 和 Laravel 使 pdf 精确到 10CM 宽度?
How to make pdf exact 10CM width with DOMPDF and Laravel?
我正在使用 DOMPDF 和 laravel 创建 PDF。
正在使用只接受 10CM 宽和 20CM 高的文件的特殊打印机打印 pdf。
我试过这个:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper);
因为 11X17 是
"11x17" => array(0, 0, 792.00, 1224.00),
我算出 10X20 是 0,0720,1440
但是没用。
感谢任何帮助。
提前致谢
我认为问题出在方向上,因为 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;
}
尝试使用:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper,'portrait');
希望能有所帮助,或者尝试其他选项而不是 portrait
。
我是如何解决这个问题的:
更改自定义纸张。
下载在 Acrobat 中打开的 PDF Reader 现在将鼠标移到左上角,您可以看到文档的宽度和高度,我相应地更改了自定义纸张。
最终结果是:
10CM X 20CM =
$customPaper = array(0,0,567.00,283.80);
$pdf = PDF::loadView('pdf.retourlabel', compact('retour','barcode'))->setPaper($customPaper, 'landscape');
非常迂回的工作,但我确实完成了工作..
谢谢
在 PHP 中设置纸张大小需要知道点 (pt) 测量值,点是原始 PDF 单位。 PDF 分辨率(使用 Dompdf)为 72pt/inch。所以 10cm x 20cm 大致等于 283 X 566(如您在 中所述)。
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
的意思是 相反的 。因此,我们可以将其设置为:array(0, 0, 566.929133858, 283.464566929 )
与答案相同但更精确的值
但是,您可以通过在 CSS 中指定页面尺寸来允许 Dompdf 计算适当的磅值。从 Dompdf 0.6.2 开始可用。
<html>
<head>
<style>
@page { size: 10cm 20cm landscape; }
</style>
</head>
<body>
...
</body>
</html>
琐事:PDF 规范未提供指示纸张方向的方法(尽管有指示旋转的方法)。当指定横向时,Dompdf 只是翻转 width/height。
我正在使用 DOMPDF 和 laravel 创建 PDF。
正在使用只接受 10CM 宽和 20CM 高的文件的特殊打印机打印 pdf。
我试过这个:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper);
因为 11X17 是
"11x17" => array(0, 0, 792.00, 1224.00),
我算出 10X20 是 0,0720,1440
但是没用。
感谢任何帮助。
提前致谢
我认为问题出在方向上,因为 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;
}
尝试使用:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper,'portrait');
希望能有所帮助,或者尝试其他选项而不是 portrait
。
我是如何解决这个问题的:
更改自定义纸张。
下载在 Acrobat 中打开的 PDF Reader 现在将鼠标移到左上角,您可以看到文档的宽度和高度,我相应地更改了自定义纸张。
最终结果是: 10CM X 20CM =
$customPaper = array(0,0,567.00,283.80);
$pdf = PDF::loadView('pdf.retourlabel', compact('retour','barcode'))->setPaper($customPaper, 'landscape');
非常迂回的工作,但我确实完成了工作..
谢谢
在 PHP 中设置纸张大小需要知道点 (pt) 测量值,点是原始 PDF 单位。 PDF 分辨率(使用 Dompdf)为 72pt/inch。所以 10cm x 20cm 大致等于 283 X 566(如您在
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
的意思是 相反的 。因此,我们可以将其设置为:array(0, 0, 566.929133858, 283.464566929 )
与答案相同但更精确的值
但是,您可以通过在 CSS 中指定页面尺寸来允许 Dompdf 计算适当的磅值。从 Dompdf 0.6.2 开始可用。
<html>
<head>
<style>
@page { size: 10cm 20cm landscape; }
</style>
</head>
<body>
...
</body>
</html>
琐事:PDF 规范未提供指示纸张方向的方法(尽管有指示旋转的方法)。当指定横向时,Dompdf 只是翻转 width/height。