Im using tinymce with image upload using the package "laravel-tinymce-simple-imageupload
". So I have a page where the user can select a registration type and enter some content in the tinymce textarea to associated that content to the selected registration type.
In the textarea the user can also upload images using the tinymce-simple-image-upload and this images are stored in "~/projects/proj/public/img/image_15....
".
The content is stored in database in the certificates table in the column "content" like:
<p>certificate test<img src="../../../img/image_1532441196_7GTrIzUnb.jpeg"
alt="" width="1200" height="900" /></p>
Error: The issue is that I have the code below to get a pdf with the certificate content associated with a specific registration and it works. But the downloaded pdf file, if the certifiate content has an image, the image dont appear in the pdf, in the pdf instead of show the image show this error "Image not found or type unknown
".
Do you know why?
Code to get the certificate HTML from db in pdf:
public function getCertificateInfo($regID)
{
$participants = Participant::where('registration_id', $regID)
->whereHas('registration_type', function ($query) {
$query->where('certificate_available', 'Y');
})
->with('registration_type.certificate')
->get();
$content = '';
foreach ($participants as $participant) {
$content .=
'<div style="page-break-inside: avoid; page-break-after: always;">
'.$participant->registration_type->certificate->content.'</div>';
}
$pdf = app()->make('dompdf.wrapper');
$pdf->getDomPDF()->setBasePath(public_path().'/../../');
$pdf->loadHTML($content);
return $pdf->download('certificates.pdf');
}
Like this with the html static it works, the image appears in the downloaded pdf:
$pdf = app()->make('dompdf.wrapper');
$pdf->loadHTML('
<p>cert1<img src="'.public_path().
'/img/image_1532441196_7GT.jpeg"
alt="" width="1200" height="900" /></p>');
return $pdf->download('certificates.pdf');
TinyMce code:
tinymce.init({
selector:'textarea',
plugins: 'image code link',
relative_urls: true,
file_browser_callback: function(field_name, url, type, win) {
// trigger file upload form
if (type == 'image') $('#formUpload input').click();
}
});
The $content variable shows like:
"""
<div style="page-break-inside: avoid; page-break-after: always;">\n
<p>cert1<img src="../../../img/image_1532441196_7GT.jpeg"
alt="" width="1200" height="900" /></p></div> ▶
</div>
"""
Code in certificateController that inserts the certificate content (the content introduced in the tinymce textarea) in the DB:
public function update(Request $request){
$registrationType = RegistrationType::where('id', $request->registrationType)->first();
$certificate = $registrationType->certificate;
// if no certificate exists for this type, create it
if(!$certificate ) {
$certificate = new Certificate();
}
// the certificate_content is the textarea with the tinymce plugin
$certificate->content = $request->certificate_content;
$certificate->save();
$registrationType->certificate_id = $certificate->id;
$registrationType->save();
$certificateContent = RegistrationType::with('certificate')->where('id', $request->registrationType)->first();
Session::flash('success','Certificate configured with success for the selected registration type.');
return redirect()->back();
}
Change:
$pdf->getDomPDF()->setBasePath(public_path().'/../../');
to:
$pdf->getDomPDF()->setBasePath(public_path());
and then you just need
$pdf->loadHTML('<p>cert1<img src="/img/image_1532441196_7GT.jpeg"
alt="" width="1200" height="900" /></p>');
To get that point we need to know what is in:
$participant->registration_type->certificate->content
You have your content data stored like:
<p>certificate 1<img src="../../../img/image_1532441196_7GT.jpeg" alt="" width="1200" height="900" /></p>
To fix this issue you can update your data in database to change it to
<p>certificate 1<img src="img/image_1532441196_7GT.jpeg" alt="" width="1200" height="900" /></p>
Or you can transform content before converting it to pdf:
$content = str_replace('<img src="../../../','<img src="',$content);
$pdf->loadHTML($content);
I don't know why do you store img src with html - that is bad practice. And I don't know where else that data is in use. If this pdf generation is the only place you use that data - you can modify data in DB to keep your php code cleaner. But if you use that content somewhere else with some page widgets you will probably need to keep this data.