0

I want to generate PDF in my custom module. Using print module we can print node. But I have made html in my custom module and that I wanted to make pdf and save it in files.

I have tried custom dompfg library but its working. i wanted to do something like this:

$html = "<h1>Header</h1><P>Pdf generated.</p>";

include ("mpdf.php"); /// This will be something to include for mpdf 
$mpdf=new mPDF('win-1252','A4','','',20,15,48,25,10,10); 
$mpdf->WriteHTML($html);
$mpdf->Output('../default/files/users/filename.pdf','F'); 
exit;

Any suggestions?

2

There is quite a bit more involved in integrating Drupal with the mPDF library than the code snippet you show.

As often with Drupal, there is a module for that. It is named PDF using mPDF, and this is what it will do:

This module is used for Conversion of HTML pages to PDF documents using mPDF PHP Library. It allows you to generate a pdf document of any node.

Make sure you follow the instructions in the README.txt files carefully. You need to place the mPDF library in the right directory, and install the Libraries API, for it to work correctly.

0

You can generate PDF Using Html using dompdf

In my case I need to create a pdf from a custom menu callback. So to achieve this

Step 1 : Download dompdf library

Step 2 : Place the library in sites/all/libraries/dompdf

Step3 : Create a function like the following for download pdf for your custom html

<?php

require_once("sites/all/libraries/dompdf/autoload.inc.php");
use Dompdf\Dompdf;

function generate_custom_pdf(){

  $html = '<put your html content here>'; // you may add your content here

  $dompdf = new Dompdf();
  $dompdf->load_html($html);
  $dompdf->render();
  // This does not save the pdf field and instead it opens a dialog box asking whether you have to save the pdf or not
  $dompdf->stream("ckl-sample.pdf");
  drupal_exit();
}

?>

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.