【问题标题】:integrate Tinymce with dompdf将 Tinymce 与 dompdf 集成
【发布时间】: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-&gt;getDomPDF()-&gt;setBasePath(public_path().'/../../');
  • 项目在“~/projects/proj”中。使用 tinymce 上传的图片位于“~/projects/proj/public/img/image_15....”中。

标签: php laravel dompdf


【解决方案1】:

变化:

$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 生成是您使用该数据的唯一地方 - 您可以修改 DB 中的数据以保持您的 php 代码更清洁。但是,如果您在其他地方使用该内容和一些页面小部件,您可能需要保留这些数据。

【讨论】:

  • 感谢 $pdf 显示基本路径,如 "-basePath: "/Users/john/projects" 并且在下载的 pdf 中仍然显示错误“找不到图像或类型未知”而不是图片。
  • 你希望拥有什么而不​​是"-basePath: "/Users/john/projects
  • 谢谢,但是带有静态 html 的代码,例如 " $pdf->loadHTML('

    cert1

    ');"作品。问题是内容是静态的,但真正需要的是实际使用存储在数据库中的 html 信息。
  • 您在 OP 中写道 $content 变量显示如下: ... 哪个不起作用。它应该是图像文件的完整网址。
  • 感谢将数据库更改为“

    cert1

    " 图像出现在 pdf 中。
【解决方案2】:

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

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

之后就是这个样子了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多