6

How do I add a link to a PDF in the content, so that it downloads rather than opens in the browser windows? (I've uploaded the PDF to the media library and can insert the link but can't find an option to prevent it opening in the browser window).

Thanks,

Lucy

4 Answers 4

10

you may try to add this to your .htaccess :

<FilesMatch "\.(?i:pdf)$">
    # Force File Download
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>
3
  • this is great & much quicker than all the other options. How can o use multiple filetypes? Say test.notebook with the (?i:pdf) part?
    – v3nt
    Aug 23, 2011 at 10:35
  • it seems you can do that (?i:(pdf|notebook)) Aug 28, 2011 at 23:04
  • yes - did find that out eventually! Thanks Sébastien...
    – v3nt
    Aug 29, 2011 at 13:04
3

A PDF will be saved with mime-type of application/pdf, so if your theme has an application.php or pdf.php template file that forces download (or if you check for mime-type in your attachment.php template), you can force a download.

A pdf.php file built like this in your theme should do the trick:

<?php  if (have_posts()) : while (have_posts()) : the_post();

$pdf_title = $post->post_title;
$uploads_dir = wp_upload_dir();
$attachment_src = get_post_meta( $post->ID, '_wp_attached_file', true );
$pdf_src = path_join( $uploads_dir['path'], $attachment_src );


 header("Pragma: public"); // required
 header("Expires: 0");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Cache-Control: private",false); // required for certain browsers 
 header("Content-Type: application/pdf");
 header("Content-Disposition: attachment; filename=\"".$pdf_title."\";" );
 header("Content-Transfer-Encoding: binary");
 header("Content-Length: ".filesize($pdf_src));
 ob_clean();
 flush();
 readfile("$pdf_src");


endwhile; endif;

?>

(Edit: I should note that in order for this to work, when you insert the file into your post through the media uploader, you have to select Post URL rather than File URL in the Link URL field before inserting into your post. A link to a filename will follow the browser's preferences, but by linking to the WP post link, you can control its behaviour.)

3
  • 3
    You should never use the post's guid as a URL. It is a url only because it is a unique string; it could just as easily change. Dec 17, 2010 at 21:47
  • Thanks, I changed my function to go off the '_wp_attached_file' meta data. Dec 18, 2010 at 4:18
  • 1
    You many instead find the path to the pdf as follows: $pdf_src = get_attached_file($post->ID ); $bytes = filesize( $pdf_src );
    – Dipesh KC
    Apr 29, 2013 at 12:39
2

Actually, that's entirely dependent on the browser. Some browsers will open a PDF in the window using a built-in PDF reader (most use the Adobe plug-in, Google Chrome has its own). If the plug-in is missing, though, the browser will attempt to download the file instead.

Your safest bet is to add instructions on the page for the user to right-click and select "save as."

If you're concerned about users not being able to get back to the page after they click a link, add target="_blank" to the link and it will force the browser to open the link in a new tab or window. Sometimes, this can be enough to trigger a download instead as well.

Eg: <a href="http://site.url/document.pdf" target="_blank">Download PDF</a>.

1
  • 1
    Actually, "forcing a download" is entirely dependent on the HTTP response headers sent from the server; not the browser. However, whether a PDF can be displayed in the browser is dependent on the browser/plugins.
    – MrWhite
    Jan 24, 2015 at 12:34
0

Just use the a tag's download attribute: https://www.w3schools.com/tags/att_a_download.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.