Skip to content Skip to sidebar Skip to footer

Generate PDF File In An Appropriate Format

For my use, I created a PDF file using flying-saucer library. It was a legacy HTML so I cleaned out the XHTML using HTMLCleaner library. After this I serialize the XML as string t

Solution 1:

Your MIME type is incorrect for PDF. It should be application/pdf.

Change

response.setContentType("text/pdf");

to

response.setContentType("application/pdf");

See https://www.rfc-editor.org/rfc/rfc3778 for the RFC for the PDF MIME type.

Edit: Totally overlooked the "Save as .pdf" question. You'll also need to add something like:

response.setHeader("content-disposition", "attachment; filename=yourFileName.pdf");

to tell the browser what the default file name should be.


Post a Comment for "Generate PDF File In An Appropriate Format"