【问题标题】:passing the image to dompdf将图像传递给 dompdf
【发布时间】:2014-03-07 16:40:04
【问题描述】:

我的 save.php 中有以下代码:

<?php 
require_once("dompdf/dompdf_config.inc.php");

$LgPath=$_POST['image'];
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/png");
//header("Content-Disposition: attachment; filename=image.png");

$html = file_get_contents($LgPath);
//$html = readfile($LgPath); --only shows the image

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

我成功地使用 HTML2CANVAS 获取某个元素的打印屏幕,然后使用 $POST 将其传递到 save.php 页面。我希望使用 DOMPDF 将该图像放入 sample.pdf 中。使用此代码,我在 sample.pdf“‰PNG”中只有此文本,我不知道如何修复它。使用 readfile 我看到了图像,但就是这样......有什么建议吗?谢谢。

【问题讨论】:

    标签: pdf-generation dompdf html2canvas


    【解决方案1】:

    您正在将图像读入变量并将其传递给 dompdf。因此,dompdf 将构成该图像的文本视为 HTML 源。由于您的图像可以从存储在$LgPath 中的位置的 PHP 脚本读取,您应该能够执行以下操作:

    <?php
    require_once("dompdf/dompdf_config.inc.php");
    $LgPath=$_POST['image'];
    $html = '<img src="' . $LgPath . '">';
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream("sample.pdf");
    ?>
    

    【讨论】:

    • 与此同时,我使用了其他变体,将图像写入服务器,然后从那里将其传递给 dompdf。但我更喜欢你的变种。感谢您提供简单的解决方案,这似乎避免了我...... p.s.现在我只需要弄清楚为什么当我尝试从 sample.pdf 打印时,图像不在页面顶部,而是在页面的垂直中间:)
    • 这是一个奇怪的问题。如果图像在屏幕上显示正确,则可能是您的打印设置。查看如何将呈现的 PDF 设置为“适合”页面。如果不是这样,可能需要一个新问题。
    • 是的,图像适合页面并且正在下降..似乎如果我使用手动传递给页面尺寸的相同比例的图像,那么图像将位于顶部.我就这样离开了,即使尺寸在 A4 规格下,肉眼或打印时也看不到质量差异:)。再次感谢您的回答。
    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2019-04-17
    • 2012-03-22
    • 2016-05-29
    相关资源
    最近更新 更多