将 Tinymce 与 dompdf 集成

integrate Tinymce with dompdf

我正在使用 tinymce 使用包“laravel-tinymce-simple-imageupload”上传图片。所以我有一个页面,用户可以在其中 select 注册类型并在 tinymce 文本区域中输入一些内容以将该内容关联到 selected 注册类型。

在文本区域中,用户还可以使用 tinymce-simple-image-upload 上传图片,这些图片存储在“~/projects/proj/public/img/image_15....”中。

内容存储在数据库中的证书 table 中的 "content" 列中,例如:

<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 中获取证书 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 静态效果,图像出现在下载的 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中引入的内容)插入到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();

    }

变化:

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

至:

$pdf->getDomPDF()->setBasePath(public_path());

然后你只需要

$pdf->loadHTML('<p>cert1<img src="/img/image_1532441196_7GT.jpeg" 
alt="" width="1200" height="900" /></p>');

为了得到这一点,我们需要知道里面有什么:

$participant->registration_type->certificate->content

您的内容数据存储如下:

<p>certificate 1<img src="../../../img/image_1532441196_7GT.jpeg" alt="" width="1200" height="900" /></p>

要解决此问题,您可以更新数据库中的数据以将其更改为

<p>certificate 1<img src="img/image_1532441196_7GT.jpeg" alt="" width="1200" height="900" /></p>

或者您可以在将内容转换为 pdf 之前对其进行转换:

$content = str_replace('<img src="../../../','<img src="',$content);
$pdf->loadHTML($content);

我不知道你为什么用 html 存储 img src - 这是不好的做法。而且我不知道这些数据还用在哪里。如果此 pdf 生成是您唯一使用该数据的地方 - 您可以修改数据库中的数据以保持 php 代码更清晰。但是,如果您在其他地方使用某些页面小部件使用该内容,您可能需要保留这些数据。

您必须使用完整的服务器路径。 要解决这个问题,您必须将 ../../../ 替换为 projects/proj/public 这样做吧。

<?php str_replace('../../../','projects/proj/public',$content); ?>

之后就是这个样子

<img src="{{ public_path("projects/proj/public/img/".$ImageName) }}" alt="">