About PDF document files
With application/pdf, browsers open the file in their built-in PDF viewer. Add Content-Disposition: attachment to make them download it instead. PDF content streams are usually compressed already, so gzip on the fly saves little.
-
A PDF that downloads as
file.pdf.htmlor shows as garbage was almost always sent astext/htmlorapplication/octet-stream, often by an error page or a misconfigured proxy. -
For uploads, check that the file starts with
%PDF-; the type the browser reports is based only on the extension.
Other types seen for these files:
application/x-pdf
. Accept them when reading uploads, but send application/pdf yourself.
File signature
PDF files start with 25 50 44 46 2D ("%PDF-"). The extension and the type a browser reports for an upload both
come from the file name, so check these bytes on the server before trusting a file.
Serve .pdf with the right Content-Type
Content-Type: application/pdf
application/pdf pdf;
AddType application/pdf .pdf
@pdf path *.pdf
header @pdf Content-Type "application/pdf"
curl -sI https://example.com/file.pdf | grep -i '^content-type'
# Content-Type: application/pdf
Set the type in code
res.set('Content-Type', 'application/pdf');
// Or make the browser download it:
res.attachment('file.pdf');
import mimetypes
mimetypes.add_type('application/pdf', '.pdf') # if your system doesn't know it
mimetypes.guess_type('file.pdf')[0] # 'application/pdf'
mime.AddExtensionType(".pdf", "application/pdf")
w.Header().Set("Content-Type", "application/pdf")
Accept .pdf uploads
<input type="file" accept=".pdf,application/pdf">
The accept attribute only filters the file picker; users can still choose another file. Listing the
extension as well as the type matters, because operating systems don’t always report the type you expect.
Related types
-
.docx (
application/vnd.openxmlformats-officedocument.wordprocessingml.document): Word document -
.epub (
application/epub+zip): EPUB e-book -
.txt (
text/plain): Plain text
More documents
.doc , .xls , .xlsx , .ppt , .pptx , .odt , .rtf . Look up any extension or type with the MIME type lookup.
Sources
Spotted a mistake? Email hello@devtoolsmith.com and we’ll fix it.
Frequently asked questions
What is the MIME type for .pdf files?
application/pdf. You may also see application/x-pdf, which is older or unofficial; send application/pdf. The full header is "Content-Type: application/pdf".
Why does my server send the wrong Content-Type for .pdf?
Web servers pick the type from the file extension using a table (mime.types in nginx and Apache). If .pdf is missing from that table, they send a default such as application/octet-stream or text/plain. Add the mapping as shown on this page, then check the response with "curl -sI".
How can I check that a file really is PDF?
Look at the first bytes rather than the extension or the type the browser reports, which both come from the file name. PDF files start with 25 50 44 46 2D ("%PDF-"). The MIME type lookup on this site reads them for you when you drop a file on it.