duanchi6397 2018-07-22 21:38
浏览 163
已采纳

将Tinymce与dompdf整合在一起

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;">

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

    }
  • 写回答

2条回答 默认 最新

  • duanbinian2243 2018-07-25 14:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置