Jump to content

Template for PDF Output


kixe
 Share

Recommended Posts

You want to display pdf files as pages in frontend? Go here:

  • Create a template file 'showpdf.php'
    <?php
    $file = $page->pdf->filename;
    header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="' . basename($file).'"');
    header('Content-Length: ' . filesize($file));
    readfile($file);
  • Create field 'pdf' of type file and add it to your template.
  • Create a page using the template 'showpdf.php'
  • Add pdf to page field 'pdf'. Done.

To get save option instead of display use 'attachment' instead of 'inline' like

header('Content-Disposition: attachment; filename="' . basename($file).'"');
Edited by kixe
  • Like 7
Link to comment
Share on other sites

Just a thought on this. It looks like this will result in URLs like: mysite.com/documents/mydocument/ that will directly display a PDF. Correct?

Maybe it's just me, but I always like to know when I am about to open up a PDF - it gives me the option to save instead, or open in a new tab, or decide not to open because I am on mobile don't want to wait for a potentially huge PDF file to open. Maybe this is becoming less of an issue now that browsers are directly rendering PDFs themselves, or sites are using mozilla's pdf.js etc.

Anyway, just my two cents worth :)

PS, given that you are using a file field, shouldn't:

$file= $page->pdf;

be:

$file= $page->pdf->url;
  • Like 2
Link to comment
Share on other sites

be:

$file= $page->pdf->url;

Thanks Adrian,

finally it should be:

$file = $page->pdf->filename;

It doesn't work with url here. Don't know why? Got it: readfile() needs fullpath!

Added filesize-header. To force download it should be 'attachment' instead of 'inline'

<?php
$file = $page->pdf->filename;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
Edited by kixe
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...