简体   繁体   English

mpdf - 来自网址的图片不会加载生成的pdf

[英]mpdf - images from a url won't load in generated pdf

I'm trying to load images from a http url but they won't display in my generated pdf. 我正在尝试从http网址加载图片,但它们不会显示在我生成的pdf中。

$this->layout = '//layouts/pdftemplate';
$pdf = Yii::app()->toPDF->mpdf();
$pdf->shrink_tables_to_fit = 1;
$pdf->defaultfooterline = false;
$stylesheet = file_get_contents(Yii::app()->basePath.'/../webroot/admin/themes/admin/css/formbuilder-print.css');
$pdf->WriteHTML($stylesheet, 1);
$pdf->WriteHTML($_POST['html_string']);
$pdf->Output(sys_get_temp_dir()."/test.pdf", 'F');

I'm passing the html to the php function in an ajax call. 我在ajax调用中将html传递给php函数。 The images are on Amazon CloudFront. 这些图像位于Amazon CloudFront上。

Update 更新

Thanks to Asped and Latheesan Kanes I got the issue resolved. 感谢Asped和Latheesan Kanes,我解决了这个问题。 I also used PHP's DOMDocument class to replace the image urls with the local copy of the image. 我还使用PHP的DOMDocument类将图像URL替换为图像的本地副本。 This is for future reference if anyone also runs into a similar issue 如果有人也遇到类似的问题,这是为了将来的参考

$doc = new DOMDocument();
@$doc->loadHTML($_POST['html_string']);
$imgs = $doc->getElementsByTagname('img');

foreach ($imgs as $img){
  $src = $img->getAttribute('src');
  $name = explode('?', basename($src));
  $name = $name[0];
  $tmp = sys_get_temp_dir().'/'.$name;
  copy($src, $tmp);
  $img->setAttribute('src', $tmp);
}

$html = $doc->saveHTML(); // you can write this to the pdf. $pdf->WriteHTML($html);

I had a similar issue once displaying an SVG file in the pdf.. it would not work. 一旦在pdf中显示SVG文件,我就遇到了类似的问题..它不起作用。 Then I converted it to a PNG (on the fly), stored locally in a temp folder, and passed the temporary file to mDPF, which helped. 然后我将它转换为PNG(动态),本地存储在临时文件夹中,并将临时文件传递给mDPF,这有助于。

UPDATE - Actually now I remember I didn't even had to convert it, I just had to store it locally in a temp folder.. 更新 - 实际上现在我记得我甚至不必转换它,我只需将它本地存储在临时文件夹中..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM