【发布时间】:2018-07-22 21:38:46
【问题描述】:
我正在使用 tinymce 使用“laravel-tinymce-simple-imageupload”包上传图片。所以我有一个页面,用户可以在其中选择注册类型并在 tinymce 文本区域中输入一些内容,以将该内容与所选注册类型相关联。
在 textarea 中,用户还可以使用 tinymce-simple-image-upload 上传图片,这些图片存储在“~/projects/proj/public/img/image_15....”中。
内容存储在“内容”列中的证书表中的数据库中,例如:
<p>certificate test<img src="../../../img/image_1532441196_7GTrIzUnb.jpeg"
alt="" width="1200" height="900" /></p>
错误: 问题是我有下面的代码来获取包含与特定注册相关的证书内容的 pdf,并且它可以工作。但是下载的pdf文件,如果证书内容有图片,图片不会出现在pdf中,在pdf中不显示图片会显示这个错误“Image not found or type unknown”。
你知道为什么吗?
从 db 中以 pdf 格式获取证书 HTML 的代码:
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');
}
像这样使用 html static 它工作,图像出现在下载的 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 代码:
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();
}
});
$content 变量显示如下:
"""
<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>
"""
certificateController 中将证书内容(tinymce textarea 中引入的内容)插入数据库的代码:
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();
}
【问题讨论】:
-
您的文件夹结构是什么?您的应用程序在哪里与您的图像在哪里?你为什么要
$pdf->getDomPDF()->setBasePath(public_path().'/../../');? -
项目在“~/projects/proj”中。使用 tinymce 上传的图片位于“~/projects/proj/public/img/image_15....”中。