图片未找到或类型未知(使用带有 dompdf 的 tinymce 以 pdf 格式显示存储在 tinymce textarea 中的 html)

Image not found or type unknown (using tinymce with dompdf to show html stored in tinymce textarea in a pdf)

我使用 tinymce 使用包 "laravel-tinymce-simple-imageupload" 上传图片。当用户在 textarea 中输入一些内容并单击表单提交按钮时,我想将 textarea 中的内容放入 pdf 文件中。我有下面的代码。

问题是在 pdf 文件中,如果在文本区域中插入图像,图像不会出现在 pdf 中,而在 pdf 中出现 "Image not found or type unknown"。

你知道问题出在哪里吗?

图片在证书的内容栏中是这样存储的table:

<p>test<img src="/img/image_15zdbBr.jpeg" alt="" width="1200" height="900" /></p>

获取pdf的代码:

$certificateContent = RegistrationType::with('certificate')->where('id', $request->registrationType)->first();
$pdf = app()->make('dompdf.wrapper');
$pdf->loadHTML($certificateContent->certificate->content);
return $pdf->download('test.pdf');

Tinymce 代码 relative_urls 为假:

tinymce.init({
    selector:'textarea',
    plugins: 'image code link',
    relative_urls: false,
    file_browser_callback: function(field_name, url, type, win) {
        // trigger file upload form
        if (type == 'image') $('#formUpload input').click();
    }
});

我已经在使用 "setOptions(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true])",但也无法使用它,它显示相同的错误。

看来问题可能是因为有必要更改图像的 url 路径。但是我不明白该怎么做,因为用户只在 tinymce textarea 中选择一个图像如何更改该图像的绝对路径。

这是一个已报告的问题:https://github.com/dompdf/dompdf/issues/1659 .

建议您:

  1. 将 relative_urls 设置为 true(这会将图像设置为 img/image_15zdbBr.jpeg,并且
  2. set $dompdf->setBasePath($base_path) 其中 $base_path 是文件所在的位置。 (https://github.com/dompdf/dompdf/wiki/Usage#setbasepath)

使用聊天中的重要信息进行编辑:

  1. 当您使用 Laravel 的包装器时,您需要获得 domPDF class 的句柄,您将通过 $pdf->getDomPDF()->setBasePath();
  2. 因为相对文件路径是../../../image/file.jpg,也就是说"start at the "base目录,往回走,往回走,往回走,进入img/,找到文件”。所以"base directory" 需要在文件之前考虑您要返回的事实。

工作示例:

  • 您的实际文件位于(真实的)/home/john/projects/proj/public/img/image.jpeg
  • 相对文件路径(已提供)=../../../img/image.jpeg
  • 所以你配置你的setBasedirectory = /home/john/projects/proj/public/a/b/c/
  • 结合这给你 /home/john/projects/proj/public/a/b/c/../../../img/image.jpeg
  • /home/john/projects/proj/public/a/b/../../img/image.jpeg
  • 相同
  • /home/john/projects/proj/public/a/../img/image.jpeg
  • 相同
  • 这与 /home/john/projects/proj/public/img/image.jpeg = Bingo 相同。

如果这不起作用,请调整您的 setBaseDirectory 直到获得正确的路径。