-->

Image not found or type unknown (using tinymce wit

2019-07-09 23:38发布

问题:

I'm using tinymce with image upload using the package "laravel-tinymce-simple-imageupload". When the user enters some content in the textarea and clicks the form submit button I want to put the content in the textarea in a pdf file. I have the code below.

The issue is that in the pdf file, if is inserted an image in the textarea, the image doesn't appear in the pdf and in the pdf appears "Image not found or type unknown".

Do you know what can be the issue?

The image is stored like this in the content column of the certificates table:

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

Code to get the 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 code has relative_urls as false:

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();
    }
});

I already use "setOptions(['isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true])" but also dont works with that, it show the same error.

It seems that the issue can be because is necessary to change the url path of the image. But Im not understanding how to do that since the user only selects a image in the the tinymce textarea how to change the absolute path of that image.

回答1:

This is a reported issue: https://github.com/dompdf/dompdf/issues/1659 .

Suggest you:

  1. set relative_urls to true (this will set the image as img/image_15zdbBr.jpeg, and
  2. set $dompdf->setBasePath($base_path) where $base_path is where the files sit. (https://github.com/dompdf/dompdf/wiki/Usage#setbasepath)

Edit with salient bits from chat:

  1. As you are using a wrapper for Laravel, You need to get a handle on the domPDF class which you will do through $pdf->getDomPDF()->setBasePath();
  2. As the relative file path is ../../../image/file.jpg, this means "start at the "base directory, go back one, go back one, go back one, go into img/, find file". So the "base directory" needs to be ahead of the the files to take accound for the fact you're going back.

Working example:

  • Your actual file is located (for real) in /home/john/projects/proj/public/img/image.jpeg
  • Relative file path (provided) = ../../../img/image.jpeg
  • So you configure your setBasedirectory = /home/john/projects/proj/public/a/b/c/
  • Combined this gives you /home/john/projects/proj/public/a/b/c/../../../img/image.jpeg
  • Which is the same as /home/john/projects/proj/public/a/b/../../img/image.jpeg
  • Which is the same as /home/john/projects/proj/public/a/../img/image.jpeg
  • Which is the same as /home/john/projects/proj/public/img/image.jpeg = Bingo.

If this doesn't work, adjust your setBaseDirectory until you get the right path.