adamjhn's avatar

How to get the basepath?

Im using tinymce with image upload using the package "laravel-tinymce-simple-imageupload". But when the user enters some content in the textarea and click in the form submit button I want to put the content in the textarea in a pdf file. The issue is that in the pdf file, if is inserted an image in the textarea, the image dont appear in the pdf and it shows "Image not found or type unknown.

The image is stored in the database like

<p>cert1<img src="../../../img/image_1532441196_7GTrIznNyYAnzHbOEAYcpU.jpeg" alt="" width="1200" height="900" /></p>

The directory of the image uplodaded with the tinymce plugin is "~/projects/proj/public/img/image_15....".

Do you know how to get the basepath to use in the setBasePath method $pdf->getDomPDF()->setBasePath() to check if the error "Image not found or type unkown" is solved using the serBasePath()?

0 likes
7 replies
bobbybouwmann's avatar

Well, if your image is stored in the public directory you can simply use the public_path() helper or use the base_path() helper

<img src="{{ public_path('img/image_1531443_Yn7tAZmzcbb82UOGHo07eIctkBjh98r.jpeg') }}"/>

<img src="{{ base_path('public/img/image_1531443_Yn7tAZmzcbb82UOGHo07eIctkBjh98r.jpeg') }}"/>

Remember that paths in html files are already relative to the root directory of the project and not to the file itself. In the case of Laravel your public directory is the root directory for your website.

1 like
adamjhn's avatar

Thanks, something like this?


$pdf = app()->make('dompdf.wrapper');


$pdf = $pdf->getDomPDF()->setBasePath(public_path().'/img/');

//dd($pdf);

$pdf = PDF::loadHTML($content);

return $pdf->download('certificates.pdf');

Because like that when the there is an image in the certificate it appears "Image not found or type unknown" in the pdf instead of the image.

The dd($pdf); shows:


Dompdf {#332 ▼
  -version: "dompdf <5113accd>"
  -dom: null
  -tree: null
  -css: Stylesheet {#339 ▶}
  -canvas: CPDF {#345 ▶}
  -paperSize: "a4"
  -paperOrientation: "portrait"
  -callbacks: []
  -cacheId: null
  -baseHost: ""
  -basePath: "/Users/ajohn/projects/proj/public/img/"
  -protocol: null
  -httpContext: null
  -startTime: null
  -systemLocale: null
  -localeStandard: true
  -defaultView: "Fit"
  -defaultViewOptions: []
  -quirksmode: false
  -allowedProtocols: array:5 [▶]
  -allowedLocalFileExtensions: array:2 [▶]
  -messages: []
  -options: Options {#335 ▶}
  -fontMetrics: FontMetrics {#340 ▶}
}
Cronix's avatar
$pdf = PDF::loadHTML($content);

There you're using the PDF class directly, instead of using the $pdf instance you already created (and set values for). This is a separate instance. Use $pdf there, not PDF.

1 like
adamjhn's avatar

Thanks, but like that appear "Non-static method Dompdf\Dompdf::loadHtml() should not be called statically ".

Cronix's avatar

Like what? I said to not use PDF:: and only posted the code where you were doing it.

use the $pdf instance you already have so you don't overwrite your original object with a new one (which doesn't have the settings you previously set on $pdf).

$pdf = app()->make('dompdf.wrapper'); // $pdf is now a PDF instance

$pdf->getDomPDF()->setBasePath(public_path().'/img/');

//$pdf = PDF::loadHTML($content); // don't create a NEW instance, use the existing $pdf instance
$pdf->loadHTML($content);

return $pdf->download('certificates.pdf');
1 like
adamjhn's avatar

Thanks like that dont shows that error but still appear the error "Image not found or type unknown".

Cronix's avatar

Ok, don't know how to help with that. I just saw some things you were doing wrong.

Please or to participate in this conversation.