首页 文章

MPDF文档和图像DPI问题

提问于
浏览
1

背景:我正在尝试使用mPDF库以300dpi渲染名片 . 该文档具有图像背景,该图像背景应填充画布,然后覆盖各种文本元素 .

尺寸:PDF文档设置为91mm x 61mm,采用横向格式 .

$pdf = new mPDF('utf-8', array(91,61), 0, '', 0, 0, 0, 0, 0, 'L');

我在配置中设置的分辨率如下;

$this->dpi = 300; $this->img_dpi = 300

我在photoshop中创建了一个图像,也是300dpi,与卡的尺寸相同(91mm x 61mm) .

我尝试使用标记将图像添加到div内的文档,如下所示:

$html .= '<div style="position: absolute; left:0; right: 0; top: 0; bottom: 0;width:100%; height:100%"><img style="width:100%; height:100%" src="/assets/images/bg.jpg"></div>';

显示PDF文档时,图像似乎小于文档,即,它不会缩放以适合页面 . 实际上,图像仅在X和Y方向上填充大约55-60%的画布 .

当我保存PDF文档并在Adobe阅读器中查看其属性时,确认其大小为91x61mm .

有没有人有类似的问题或了解这里发生了什么?

我真的需要能够拥有一个300dpi的图像,它将完全填满页面 .

我期待着你的建议 .

问候

詹姆士

1 回答

  • 1

    您所拥有的css样式与'size constraint'部分中的手册recommendation中的css样式完全不匹配

    在我的情况下,我有一个在photoshop中的11x8.5英寸150dpi的风景图像,它与mpdf匹配作为页面大小,我将css分开清洁

    为了以防万一,我指定了任何其他mpdf值或css设置

    所以尝试更像这个适合我的代码,用css定位文本并根据需要调整dpi / page大小:

    $html = '
    <html>
    <head>
    <style>
        body {
            position: relative;
            text-align: center;
            overflow: hidden;
            page-break-inside: avoid;
        }
        #bg {
            position: absolute;
            left:0;
            right: 0;
            top: 0;
            bottom: 0;
        }
        #bg img {
            width: 100%;
            height: 100%;
            margin: 0;
        }
        #text1 {
            top: 20%;
            font-size: 20pt;
            position: absolute;
            width: 100%;
            color: #fff;
        }
        #text2 {
            top: 60%;
            font-size: 16pt;
            position: absolute;
            width: 100%;
            color: #ddd;
        }
    </style>
        </head>
        <body>
        <div id="bg">
            <img src="bg.png" />
        </div>
        <div id="text1">John Smith</div>
        <div id="text2">555-5555</div>
    </body>
    </html>';
    
    $mpdf = new mPDF('UTF8','Letter-L',14,'Arial',0,0,0,0,0,0);
    $mpdf->dpi = 150;
    $mpdf->img_dpi = 150;
    $mpdf->WriteHTML($html);
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->Output();
    

    有一个更简单的方法来制作一个背景...告诉body或html在css中有一个背景图像,但是你遇到this bug所以这不是我的第一个建议

相关问题