douyanpeng0748 2015-10-23 10:36
浏览 80

为什么dompdf不遵守指定的页面大小?

I'm trying to create a PDF using dompdf version 0.6.1 (latest at the time of writing) and am stuck at very initial step, the page size. I'm trying to create A4 size PDF. Following is my markup

$html = <<<HTML
<html>
    <head>
       <style>
          @page {size: 21cm 29.7cm; margin: 0;}
       </style>
    </head>
    <body>
        testing
    </body>
</html>
HTML;


  $dompdf = new DOMPDF();
  $dompdf->set_paper('a4', 'portrait');
  $dompdf->load_html($html);
  $dompdf->render();

  $dompdf->stream("title.pdf");  

The PDF is created but it is always 32.07cm wide. I've tried to play around with dompdf DPI settings by overriding the DOMPDF_DPI constant but it only affects the content scaling, not the page size. Setting size CSS property value to a4 doesn't help either.

I've exhausted my searching options but havn't found a solution. Any help will be greatly appreciated. Thanks.

Update
The duplicate question doesn't have an accepted answer and the better looking among the two answers has following as a solution

$dompdf->set_paper(array(0, 0, 595, 841), 'portrait');  

the array parameter provided to DOMPDF::set_paper() is just an alternative representation of A4 size in points. I've tried it and it doesn't work. The actual issue here is that no matter how one sets the paper size, the PDF size is always 152% of it.

Update 2
I'm having same issue as "Kornel N" writes in this forum post https://groups.google.com/forum/#!topic/dompdf/I_j6R4_9ZwA

I switched to dompdf recently, I was using ufpdf before. I got used to the size of an A4 document when creating some invoices with tables.

Now, when I set the paper size to A4 ($dompdf->set_paper('a4', 'portrait');) I get a much bigger area.

I extended the write_log method with the paper size, it says it's A4, as I set it.

The cpdf_adapter.cls.php file states the size of the A4 is 595.28,841.89 points, as I got used to it.

When I open the generated pdf with pdf reader software, it says it's size is 8.27 x 11.69in, that is the size of an A4 sheet.

Still, when I try to draw a rectangle to fill the sheet (just a div with a 1px border) I have to make it 791px x 1101px big to match the paper size.

The bottom border is then about 20px from the bottom of the page, but if I try to increase the height of the div dompdf just adds one more blank page to the document, like there was an invisible bottom margin for the div, or padding for the page. There is no such, because I set it in the css.

@page { margin: 0in 0in 0in 0in;} 
.oldal{width:791px; height:1101px;  border:1px solid red;}

Please, help me, why is that happening? Is this size OK or should it be as defined?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探