5

I would like to theme the PDF generated by the Print module. I would like to add header, footer, margins, border to the generated PDF.

The certificate module in Drupal provides theming to the certificate, but my site is almost like a read and get certified site, so I wish not to change print module.

I tried the form alter and other stuffs, but not sure from where the PDF can be changed.

9
  • Have u tried TCPDF?
    – harshal
    Commented Nov 12, 2013 at 5:05
  • If you want to theme pdf as per your site design, then you need to use wkhtmltopdf to generate pdf template. Commented Nov 12, 2013 at 5:10
  • @Harshal : Yes I am using TCPDF, but still I am not sure where the contents get generated in the PDF. There exits no hook or alter to explain this.
    – Gladiator
    Commented Nov 12, 2013 at 5:13
  • @JayendraKainthola: I am using TCPDF, that should work. But still I will try the wkhtmltopdf.
    – Gladiator
    Commented Nov 12, 2013 at 5:14
  • 1
    blog.yamandi.com/print-module-drupal-generate-pdf
    – Bala
    Commented Dec 12, 2013 at 7:07

4 Answers 4

2
+250

Drupal 7 allows to alter how content is rendered with theme_print_pdf_tcpdf_content(). This should be fairly simple: Just place this hook in the template.php file, and add a new function where the PDF file is rendered. Use the following code as reference.

function theme_print_pdf_tcpdf_content(&$vars) {  
  return new_function_that_prints_the_content($vars); 
}
2

You should on your creation of node call a function which will generate a pdf. Use tcpdf class for it generate pdf and download tcpdf functions./ Check also the link

 class my_pdf extends TCPDF { //Page header public function Header() { } // Page footer public function Footer() { } } 

$pdf = new my_pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, UTF-8', false);

$pdf->setPrintHeader(true);
 $pdf->setPrintFooter(true); 
$pdf->SetMargins(25.0, 40, 25.0);
$pdf->SetFooterMargin(1.5);
$pdf->SetAutoPageBreak(TRUE, 50);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, ''); 
$pdf->lastPage();
$pdf->Output('my_document.pdf', 'D');
3
  • @Gladiator have u tried this?
    – harshal
    Commented Nov 12, 2013 at 5:26
  • Not yet will try now and will keep you updated. Thanks for the code.
    – Gladiator
    Commented Nov 12, 2013 at 5:36
  • check the link for the explanation..
    – harshal
    Commented Nov 12, 2013 at 5:40
2

Print module comes with a print-pdf.tpl.php template as well as a print.tpl.php template (or use the one in your theme). Since this is all about styling, rather look at editing these and put them in your theme folder.

1

It's actually possible using a hook for TCPDF:

/**
 * Implements hook__print_pdf_tcpdf_alter()
 */
function MODULENAME_print_pdf_tcpdf_alter($pdf, $html, $meta) {
  $pdf->setPrintHeader(FALSE);
  $pdf->SetPrintFooter(FALSE);
}

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.