出于某种原因,DomPDF不会渲染正在解析的html中包含的图像:
但是,当图像以html:
返回时,图像将在页面上呈现
我已经查看了这些问题并确保将DOMPDF_ENABLE_REMOTE设置为true并验证了文件权限:
dompdf image not real image not readable or empty
Image error in DOMPDF for ZF2
还有其他我应该检查的事情吗?
答案 0 :(得分:42)
以下帮助我喜欢魅力,至少是localy,甚至是
def("DOMPDF_ENABLE_REMOTE", false);
解决方案是将图像SRC更改为服务器上的绝对路径,如下所示:
<img src="/var/www/domain/images/myimage.jpg" />
以下所有内容对我有用:
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'/placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'\placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'./placeholder.jpg';?>"/>
$ _ SERVER [“DOCUMENT_ROOT”]是C:/ wamp / www / ZendSkeletonApplication / public
答案 1 :(得分:10)
好 使用图像时遇到了同样的问题:
<img id="logo" src="/images/flags/fr.png" width="50" alt="Logo">
但是,如果我添加一个。之前/ images,在dompdf_config.custom.inc中没有更改任何内容,它可以正常工作
<img id="logo" src="./images/flags/fr.png" width="50" alt="Logo">
希望有所帮助
答案 2 :(得分:8)
由于还有另一个答案建议在module.config.php
中启用远程选项而我还无法添加评论,我认为最好回答这个文件在较新版本的DomPDF中不存在。< / p>
如果您需要在较新版本中包含远程存储的图像,则必须将其作为选项传递给构造函数:
$dompdf = new Dompdf(array('enable_remote' => true));
这解决了我遇到的问题。
答案 3 :(得分:4)
现在(2018年5月)正确的方法是:
$options = new Options();
$options->set('isRemoteEnabled',true);
$dompdf = new Dompdf( $options );
答案 4 :(得分:2)
路径:
供应商/恐龙/ DOMPDF模块/配置/ module.config.php
更改设置
enable_remote'=&gt;假,
тоtrue。
答案 5 :(得分:2)
这里没有一个解决方案适合我。相反,我只是base64编码图像,然后它工作。您可以使用此tool。
答案 6 :(得分:2)
如果您的所有图像以及其他图像都在执行脚本的同一台服务器上,则您实际上不需要打开 isRemoteEnabled
。
DomPdf 保护您免受攻击。根据 documentation,以下内容不起作用:
$dompdf = new Dompdf();
$dompdf->getOptions()->getChroot(); // something like 'C:\\laragon\\www\\your-local-website\\vendor\\dompdf\\dompdf'
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<img src="C:\\laragon\\www\\your-local-website\\public\\img\\logo.png">
</body>
</html>
HTML;
$dompdf->loadHtml($html);
您应该使用
将 CHROOT 更改为您想要的绝对路径$dompdf->getOptions()->setChroot("C:\\laragon\\www\\your-local-website\\public");
然后您可以从 <img>
文件夹内(可以嵌套)将任何带有 src
的 /public
插入到 HTML 中。
将 Chroot 设置为您的应用根文件夹似乎很容易,但不要。它打开了一扇讨厌的门,你想把它关上。据说 /public
中没有关键脚本,只有图像、公共文档、路由等。
请注意,我使用了与文档中使用的目录分隔符不同的目录分隔符。我相信最好的做法是做一些事情:
define('DS', DIRECTORY_SEPARATOR);
$public = ABSPATH . DS . 'public'; // ABSPATH is defined to be __DIR__ in root folder of your app
$image = $public . DS . 'logo' . DS . 'logo-md.png';
/* Optional */
$saveLocation = ABSPATH . DS . '..' . DS . 'private' . DS . 'invoices'; // Note that save location doesn't have to be in '/public' (or even in 'www')
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
font-family: DejaVu Sans !important;
}
@page {
margin: 0;
padding: 0;
}
html, body {
margin: 0;
min-height: 100%;
padding: 0;
}
</style>
</head>
<body>
<img src="{$image}">
</body>
</html>
HTML;
$dompdf = new Dompdf();
$dompdf->getOptions()->setChroot($public);
$domPdf->loadHtml($html, 'UTF-8');
$domPdf->setPaper('A4', 'portrait');
$domPdf->render();
/* either */
$domPdf->stream($filename); // filename is optional
/* or */
$outputString = $domPdf->output();
$pdfFile = fopen($saveLocation, 'w');
fwrite($pdfFile, $outputString);
fclose($pdfFile);
答案 7 :(得分:1)
我通过使用外部CSS的完整路径解决了这个问题。这个在我的linux ubuntu服务器上运行:
props.put("driver", "oracle.jdbc.OracleDriver")
props.setProperty("testOnBorrow","true")
props.setProperty("testOnReturn","false")
props.setProperty("testWhileIdle","false")
props.setProperty("validationQuery","SELECT 1 FROM DUAL")
props.setProperty("autoReconnect", "true")
<link href="{{ public_path('css/style.css') }}" />
并处理图片。
答案 8 :(得分:0)
对于我们的用例,我们必须将页面上的所有图像都转换为base64,因为pdf应该可以离线使用。我们的代码位于控制器类中,但是您可以根据需要进行修改。
代码如下:
/**
* Convert images to Base64 so it's included in the PDF.
*
* @param $html Full html render of the page.
*/
public function convertReportImagesToURI($html): string
{
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
$imgArr = array();
// Iterate over all image tags.
foreach ($tags as $tag) {
// Get the src attribute.
$imgSrc = $tag->getAttribute('src');
// Convert to base64.
$base64src = self::getImageDataURI($imgSrc);
$tag->setAttribute('src', $base64src);
}
return $doc->saveHTML();
}
/**
* This does the actual encoding.
*/
public static function getImageDataURI($image, $mime = ''): string
{
// Director::absoluteURL('/') gets the base url of the site.
// We had to remove the leading slash of the image hence the use of substr.
// If your images have absolute urls you will need to modify this.
$imageLocation = Director::absoluteURL('/') . substr($image, 1);
// Get the image location. remove leading slash on image url.
return 'data:' . self::get_image_mime_type($imageLocation) . ';base64,' . base64_encode(file_get_contents($imageLocation));
}
/**
* https://stackoverflow.com/a/45054843
* @param $image_path
* @return string
*/
public static function get_image_mime_type($image_path): string
{
$mimes = array(
IMAGETYPE_GIF => "image/gif",
IMAGETYPE_JPEG => "image/jpg",
IMAGETYPE_PNG => "image/png",
IMAGETYPE_SWF => "image/swf",
IMAGETYPE_PSD => "image/psd",
IMAGETYPE_BMP => "image/bmp",
IMAGETYPE_TIFF_II => "image/tiff",
IMAGETYPE_TIFF_MM => "image/tiff",
IMAGETYPE_JPC => "image/jpc",
IMAGETYPE_JP2 => "image/jp2",
IMAGETYPE_JPX => "image/jpx",
IMAGETYPE_JB2 => "image/jb2",
IMAGETYPE_SWC => "image/swc",
IMAGETYPE_IFF => "image/iff",
IMAGETYPE_WBMP => "image/wbmp",
IMAGETYPE_XBM => "image/xbm",
IMAGETYPE_ICO => "image/ico");
if (($image_type = exif_imagetype($image_path))
&& (array_key_exists($image_type, $mimes))) {
return $mimes[$image_type];
} else {
return '';
}
}
答案 9 :(得分:0)
您可以使用base64编码的图像
<img src="{{'data:image/png;base64,' . base64_encode(file_get_contents(@$image))}}" alt="image" >