【问题标题】:DOMPDF PNG output - multiple PNGs displaying only one PNG with incorrect coloursDOMPDF PNG 输出 - 多个 PNG 仅显示一个颜色不正确的 PNG
【发布时间】:2013-03-21 19:30:34
【问题描述】:

DOMPDF 对 PNG 文件的渲染存在一些严重的令人费解的问题。

根据他们的 Github 页面,在 Windows IIS 服务器上使用最新版本的 DOMPDF。


我有两个问题:

  1. PNG 图像以黑色背景和白色前景显示,无论图像的颜色如何
  2. 如果我使用多个 PNG 图片,页面上的所有图片都显示为第一张图片,并被压缩成不同的大小比例

这里有一个截图来解释:

现在,如果我用 JPEG 文件替换其中一张图片,它就可以工作了:

如果我更改要渲染的第一个 PNG 图像,它会显示如下:


我使用的 HTML 如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
        <title>test</title>

        <style type="text/css">

            *{
                color:'.$font.';
                font-family:Helvetica, Arial, Serif;
            }

            .i{font-style: italic;}
            .b{font-weight:bold;}

            h1, h2, h3, h4, h5, h6{display:inline;}
            h1{font-size:350%;}
            h3{font-size:275%;}
            h6{font-size:150%;}
            span{font-size:125%;}
            div{background-color:'.$bg.';}

        </style>
    </head>

    <body>

        <div align="center" style="margin-left:10px;margin-right:10px;padding:0px;">
            <br />
            <br />
            <br />
            <br />
            <img src="img/banner/'.$type.'/top.png" />
            <br />
            <br />
            <h1 class="b" style="color:'.$font.';">'.$type.' Certificate</h1>
            <br />
            <br />
            <h3 class="i" style="color:'.$font.';">'.$name.'</h3>
            <br />
            <br />
            <h6 style="color:'.$font.';">This certificate has been awarded to you for earning over '.$points.' points on the LEAP System.</h6>
            <br />
            <br />
            <img src="img/barry.png" />
            <br /><br />
            <img src="img/badge.png" />
            <br />
            <h6 class="i">' . 'Date Awarded: '.date("d-m-Y").'</h6>
            <br />
            <br />
            <span>Awarded by: '.$school.'</span>
            <br />
            <br />
            <img src="img/banner/'.$type.'/bottom.png" />
            <br />
            <br />
            <br />
            <br />
        </div>

    </body>
</html>

我用来生成 PDF 的 PHP 代码如下:

public function save($data, $size, $orientation, $name, $type){
    $dompdf = new DOMPDF();
    $dompdf->set_paper($size, $orientation);
    $dompdf->load_html($data);
    $dompdf->render();
    $data = $dompdf->output();

    $filename = 'data/'.$type.'/'.$name.' - '.date("d-m-Y").'.pdf';
    return file_put_contents($filename, $data);
}

生成证书后,我在dompdf\lib\fonts\log.htm得到以下登录:

52 7936.00 KB 1073.05 ms OFF
[__construct img/banner/Bronze/top.png][__construct img/barry.png][__construct img/badge.png][__construct img/banner/Bronze/bottom.png]get_min_max_width() auto auto;758.85850393701pt auto;773.85850393701pt auto;1010 121|757.5 90.75;get_min_max_width() auto auto;758.85850393701pt auto;773.85850393701pt auto;115 100|86.25 75;get_min_max_width() auto auto;758.85850393701pt auto;773.85850393701pt auto;187 60|140.25 45;[image:C:\Inetpub\wwwroot\frog\certificate_generator/img/banner/Bronze/top.png|1010|121|3]!!!png!!![addImagePng .png]
Warning: unlink(.png) [function.unlink]: No such file or directory in C:\Inetpub\wwwroot\frog\certificate_generator\dompdf\lib\class.pdf.php on line 4046
[image:C:\Inetpub\wwwroot\frog\certificate_generator/img/barry.png|115|100|3]!!!png!!!
Warning: unlink(.png) [function.unlink]: No such file or directory in C:\Inetpub\wwwroot\frog\certificate_generator\dompdf\lib\class.pdf.php on line 4046
[image:C:\Inetpub\wwwroot\frog\certificate_generator/img/badge.png|187|60|3]!!!png!!!
Warning: unlink(.png) [function.unlink]: No such file or directory in C:\Inetpub\wwwroot\frog\certificate_generator\dompdf\lib\class.pdf.php on line 4046
get_min_max_width() auto auto;758.85850393701pt auto;773.85850393701pt auto;1010 121|757.5 90.75;[image:C:\Inetpub\wwwroot\frog\certificate_generator/img/banner/Bronze/bottom.png|1010|121|3]!!!png!!!
Warning: unlink(.png) [function.unlink]: No such file or directory in C:\Inetpub\wwwroot\frog\certificate_generator\dompdf\lib\class.pdf.php on line 4046

有谁知道是什么导致了这些看似奇怪的问题?

【问题讨论】:

  • 这很奇怪。看起来其中一张图像正在被解析为它的 alpha 通道,这就是放置的内容,尽管我不能说为什么。这可能是一个路径问题,正如您从 unlink() 调用中看到的那样,有一些不完整的文件名。这些错误似乎是 dompdf/cpdf 的内部错误,但您可以从检查 dompdf 解析的 HTML 开始。加载 HTML 后调用 $dompdf-&gt;output_html(); 以查看 dompdf 在内部使用的内容。您也可以查看 dompdf/www/setup.php 上的设置页面,看看是否有任何危险信号。

标签: php iis dompdf


【解决方案1】:

好的,原来是因为我们的服务器上没有安装 ImageMagick。 DomPDF 应该在尝试生成包含 PNG 文件的 PDF 之前检查它的存在?

仅作记录,以下是我在 IIS 上安装 ImageMagick 所需采取的步骤:

  • 从 PHP 中卸载现有的 imagick 安装和 DLL 扩展
  • here 安装imagick ImageMagick-6.6.2-10-Q16-windows-dll
  • 将最新版本的线程安全 (ts) 或非线程安全 (nts) dll,例如 php_imagick_nts.dll,添加到来自 this link 的 PHP 扩展目录。
  • extension=php_imagick_nts.dll添加到php.ini
  • 运行以下 cmd 命令将 IIS 权限添加到临时目录:cacls c:\windows\temp /E /C /G iusr_SERVERNAME:R

值得注意的是,最后一条语句在工作安装中可能被认为是不安全的。您可以在配置设置中更改 imagick 临时目录,但我个人无法使其正常工作。

【讨论】:

  • dompdf 不会使用 ImageMagick,除非您启用了 ImageMagick PHP 扩展。如果您尚未安装 ImageMagick,请不要启用 ImageMagick 扩展。
  • 另外,你根本不需要 ImageMagick。 dompdf 仅使用 GD 扩展即可完成其工作。提供 ImageMagick 支持是因为它可以提高图像处理方面的性能。
猜你喜欢
  • 2011-06-12
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 2012-05-25
  • 2012-05-09
  • 1970-01-01
  • 2011-04-24
  • 2014-02-17
相关资源
最近更新 更多