Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mPDF auto print issue

Tags:

php

mpdf

I'm using a php class, mpdf, which generates PDF's very nicely. I'm trying to get the file to automatically print (i.e., open the print dialog) when rendered. I've extended the core functioning with the code below to add javascript to the pdf. The pdf is rendered but without auto-printing. Any help would be great. Thanks!

    require('mpdf.php');
    class PDF_JavaScript extends mPDF {
        var $javascript;
        var $n_js;

        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>>');
            }
        }
    }
    class PDF_AutoPrint extends PDF_Javascript { 
        function AutoPrint($dialog=false) { //Embed some JavaScript to show the print dialog or start printing immediately
        $param=($dialog ? 'true' : 'false');
        $script="print($param);";
        $this->IncludeJS($script); } }


$mpdf = new PDF_AutoPrint('', 'Letter', 0, '', 12.7, 12.7, 14, 12.7, 8, 8);

$stylesheet = file_get_contents('eabill.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($message,2);
$mpdf->AutoPrint(true);

$mpdf->Output();
like image 414
mozgras Avatar asked Aug 24 '11 00:08

mozgras


People also ask

How do I open Mpdf in new tab?

Please try the following steps: Go to Edit (Windows), Adobe Reader / Adobe Acrobat DC (Mac) > Preferences > General > Select, Open documents as new tabs in the same window > OK. Restart the application and check.

What is Mpdf file?

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF (see CREDITS), with a number of enhancements. mPDF was written by Ian Back and is released under the GNU GPL v2 licence.

How do I add multiple CSS files to Mpdf?

php $html =file_get_contents('mpdf/test. html'); include("../mpdf. php"); $mpdf=new mPDF(); $mpdf->SetDisplayMode('fullpage','two'); // LOAD a stylesheet 1 $stylesheet = file_get_contents('assets/css/main.

How do I add a page in Mpdf?

If writing a DOUBLE-SIDED document, a conditional page-break ( $type = "E" or "O" ) will add a new page only if required to make the current page match the type (i.e. ODD or EVEN); a page-break with $type = "NEXT-ODD" or "NEXT-EVEN" will add one or two pages as required to make the current page match the type (i.e. ODD ...


1 Answers

This works for me to print generated PDF file, i used it to print website page contents without menus, banners etc just content with own header and footer

$header = 'Document header';
$html   = 'Your document content goes here';
$footer = 'Print date: ' . date('d.m.Y H:i:s') . '<br />Page {PAGENO} of {nb}';

$mpdf = new mPDF('utf-8', 'A4', 0, '', 12, 12, 25, 15, 12, 12);
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);
$mpdf->SetJS('this.print();');
$mpdf->WriteHTML($html);
$mpdf->Output();
like image 173
DTukans Avatar answered Sep 25 '22 15:09

DTukans