Can Browsershot Stream A PDF Into A Laravel Mailable's Attachment?

80 views Asked by At

The Laravel site I'm working on has to generate several large PDFs. It is because of this fact that I'm using queued jobs to generate them. Upon completing the creation of these PDFs, I'm sending them in an email to the requester.

Currently though, when the emails arrive, they have a PDF attached that has application/octet-stream as its mime type. Those PDFs are mostly functional (they can be opened and even printed by PDF viewers). The lack of proper mime typing does cause some issues though, so I'd like to resolve this by making sure that they have the proper mime type of application/pdf.

I believe that a lot of this problem stems from the fact that I'm using a command like:

$pdf = Browsershot::url('https://example.com')->pdf()

which is intended for streaming a PDF directly to a browser. This approach was chosen because there will be no need to keep a file on disk once it's been sent via email.

Once that Browsershot call has been made, I then proceed to attach it to a mailable, trying to explicitly specify its mime type:

            $mail->subject($this->subj)
                ->attachData($pdf, $filename, ['mime' => 'application/pdf'])
                ->markdown('emails.emailExportCreated');
            ;

Doing this seems to make the mail client think that a properly typed PDF is attached, however upon further inspection of the file, it turns out that it's still an application/octet-stream. I've verified this using online mime type checkers.

These PDFs fail Laravel's mime validation rules as well, so cannot be uploaded back into the system. They also fail to render previews if uploaded into Slack.

Things I've tried so far include hardcoding the mime type during the ->attachData() call, and trying to use a 3rd party library to convert the octet-stream to a proper PDF file structure.

I've also tried using the ->savePdf() method to create a file on disk. When doing so, it gets created with the proper mime type. I could take this approach if necessary, but would rather conserve the disk space by using ->pdf() if possible.

Have you got any suggestions on what I should try in order to accomplish this?

0

There are 0 answers