由“ barryvdh / laravel-dompdf”生成并附加到电子邮件的pdf无法打开。 “无法加载PDF文档”

时间:2019-11-02 10:42:25

标签: php laravel email pdf dompdf

我使用barryvdh / laravel-dompdf生成文件并将其附加到电子邮件。我收到附有pdf的电子邮件,但无法打开-“无法加载PDF文档”。如果我使用$ pdf-> download()方法而不是$ pdf-> output()-下载的pdf文件没有错误。

PdfService.php:

 public function sendPdf($user, $type)
    {
        $pdf = PDF::loadView('pdf.attachment', ['type' => $type]);

        Mail::to($user)->send(new \App\Mail\Attachment($user, $pdf->output()));
    }

\ App \ Mail \ Attachment.php:

    public $user;
    public $attachment;

    /**
     * Create a new message instance.
     *
     * @param User $user
     * @param string $attachment
     */
    public function __construct(User $user, string $attachment)
    {
        $this->user = $user;
        $this->$attachment = $attachment;
    }

public function build()
    {
        return $this
            ->from('test@test.com')
            ->markdown('emails.attachment')
            ->attachData($this->attachment, 'attachment.pdf');
    }

Pdf \ Attachment.blade.php:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
    <h1>test</h1>
</body>
</html>

预期:带有“文本”内容的电子邮件附件。实际:附加到电子邮件的附件.pdf文件无法打开。“无法加载PDF文档”。

0 个答案:

没有答案