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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装