6

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.

  • Have u tried TCPDF? – harshal Nov 12 '13 at 5:05
  • If you want to theme pdf as per your site design, then you need to use wkhtmltopdf to generate pdf template. – Jayendra Kainthola Nov 12 '13 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 Nov 12 '13 at 5:13
  • @JayendraKainthola: I am using TCPDF, that should work. But still I will try the wkhtmltopdf. – Gladiator Nov 12 '13 at 5:14
  • 1
2
+250

Drupal 7 provides the option to alter the contents with the help of this API. This should be fairly simple, just place this hook in your template.php and add a new function from which the PDF should be rendered. Use the below code as reference.

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

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.

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');
  • @Gladiator have u tried this? – harshal Nov 12 '13 at 5:26
  • Not yet will try now and will keep you updated. Thanks for the code. – Gladiator Nov 12 '13 at 5:36
  • check the link for the explanation.. – harshal Nov 12 '13 at 5:40
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, privacy policy and cookie policy

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