Search code examples
javascriptphphtmlpdffpdf

Parse Html and Javascript together on FPDF


I want to generate a PDF using html (with php) and run javascript as well. I came across FPDF and I achieved both separately with libraries.

For html, html parser:

require('WriteHTML.php');

$pdf=new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr><button>LaLaLa</button>');
$pdf->Output();
?>

This works. (except the <button></button> tag; in fact <a href="test.php">Test<a> works)

For javascript, this example:

require('pdf_js.php');

class PDF_AutoPrint extends PDF_JavaScript {
  function AutoPrint($dialog=false) {
      $param=($dialog ? 'true' : 'false');
      $script="print($param);";
      $this->IncludeJS($script);
  }
}

$pdf=new PDF_AutoPrint();
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Text(90, 50, 'Print me!');

$pdf->AutoPrint(true);
$pdf->Output();
?>

When instantiating $pdf at first, both examples requires to call on their class - $pdf=new PDF_AutoPrint(); and $pdf=new PDF_HTML();.

What should I do to combine both and place button in html with javascript action to use in FPDF?


Edit: Using this will only bring me one of the Outputs, not both (output behaves like return)

$pdf=new PDF_HTML();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('You can<br><p align="center">center a line</p>and add a horizontal rule:<br><hr><a href="lala.php">jfdskjfjksd</a>');

$pdf2=new PDF_AutoPrint();
$pdf2->AddPage();
$pdf2->SetFont('Arial','',20);
$pdf2->Text(90, 50, 'Print me!');
//Open the print dialog
$pdf2->AutoPrint(true);


$pdf->Output();
$pdf2->Output();

Solution

  • In this particular scenario since the source for the PDF_JavaScript class is so small you could just add those methods to your PDF_AutoPrint class which would extend PDF_HTML.

    class PDF_AutoPrint extends PDF_HTML{
    
      function AutoPrint($dialog=false) {
          $param=($dialog ? 'true' : 'false');
          $script="print($param);";
          $this->IncludeJS($script);
      }
    
      function IncludeJS($script) {
        $this->javascript=$script;
      }
    
      function _putjavascript() {
        $this->_newobj();
        $this->n_js=$this->n;
        $this->_out('<<');
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
        $this->_out('>>');
        $this->_out('endobj');
        $this->_newobj();
        $this->_out('<<');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));
        $this->_out('>>');
        $this->_out('endobj');
      }
    
      function _putresources() {
        parent::_putresources();
        if (!empty($this->javascript)) {
            $this->_putjavascript();
        }
      }
    
      function _putcatalog() {
        parent::_putcatalog();
        if (!empty($this->javascript)) {
            $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
        }
      }
    
    }
    

    Note: Also check the license for PDF_JavaScript to make sure something like this is allowed.

    Note: If you are using php >= 5.4.0 it would be better to implement something like this using Traits