Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF export has wrong MIME type #1976

Open
sparmin opened this issue Oct 3, 2024 · 8 comments
Open

PDF export has wrong MIME type #1976

sparmin opened this issue Oct 3, 2024 · 8 comments
Labels
bug Something isn't working unconfirmed

Comments

@sparmin
Copy link

sparmin commented Oct 3, 2024

  • Lunar version: 1.0.0-beta.2
  • Laravel Version: 11.23.5
  • PHP Version: 8.3
  • Database Driver & Version:

Expected Behaviour:

Exported pdf file should have MIME type "application/pdf".

Actual Behaviour:

MIME type is "application/octet-stream". This results in validation errors on other web forms.

Steps To Reproduce:

Go on "Orders". Select an order. Download order as PDF. Check MIME type with online tools.

@sparmin sparmin added bug Something isn't working unconfirmed labels Oct 3, 2024
@glennjacobs
Copy link
Contributor

Could you clarify what you mean by...

This results in validation errors on other web forms.

Thanks

@sparmin
Copy link
Author

sparmin commented Oct 3, 2024

If the file needs to be uploaded on another website, where the form validates the file type with mime_content_type(), it results in "application/octet-stream" which can fail the validation.

@glennjacobs
Copy link
Contributor

So do you have this automated somehow?

@sparmin
Copy link
Author

sparmin commented Oct 3, 2024

No, I found that by chance. I used the file to test a form of a website I work on and thought that the form broke but instead it uses the said method and fails to validate the file. I haven't tested it with other forms on other sites, but if they do check the MIME type and don't rely only on checking the file extension (which is not a safe way), it will fail there too. In any case the MIME type should match with the file suffix to prevent any possible errors.

@glennjacobs
Copy link
Contributor

I believe that mime type is used to force the download of the PDF.

@sparmin
Copy link
Author

sparmin commented Oct 4, 2024

Sorry, I don't understand your point. What are you suggesting?

@glennjacobs
Copy link
Contributor

I mean the current set-up using "application/octet-stream" is intended, as we wanted the PDF to download in a browser, not open in the browser.

Happy to have a discussion here to determine if any changes are required.

@alexmartinfr
Copy link

alexmartinfr commented Oct 6, 2024

There is a way to preserve the correct application/pdf MIME Type while still having the browser download the file.

It involves setting the Content-Disposition header value to attachment.
On the other hand, setting that value to inline will let the browser open the PDF with its integrated viewer, if possible.

Here's how it is managed in barryvdh/laravel-dompdf:

    /**
     * Make the PDF downloadable by the user
     */
    public function download(string $filename = 'document.pdf'): Response
    {
        $output = $this->output();
        $fallback = $this->fallbackName($filename);

        return new Response($output, 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => HeaderUtils::makeDisposition('attachment', $filename, $fallback),
            'Content-Length' => strlen($output),
        ]);
    }

    /**
     * Return a response with the PDF to show in the browser
     */
    public function stream(string $filename = 'document.pdf'): Response
    {
        $output = $this->output();
        $fallback = $this->fallbackName($filename);


        return new Response($output, 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => HeaderUtils::makeDisposition('inline', $filename, $fallback),
        ]);
    }

I tried to look at Lunar's source code for the PDF generation, but can't seem to find where the 'application/octet-stream' Content-Type is defined. If you can point me in the right direction I'd be happy to look at that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed
Projects
None yet
Development

No branches or pull requests

3 participants