How to print in pdf using php with canvas element

I am trying to create an embedded PDF generator using PHP, but so far I have one problem: the pages that I am trying to create in PDF format have a histogram generated by the fleet using the canvas.

Does anyone have any experience with this or know how to get a printable canvas in pdf?

I am also open to simply uploading my client to a page on which each report graph is broken into a (printed) page using CSS to break the pages and use the built-in Print to PDF printer.

+3
source share
2 answers
//Here is the way to generate html into canvas into PDF

1 add can js files
jquery.js
JS/html2canvas.js
JS/jquery.plugin.html2canvas.js

2.get the html content to convert into canvas
        $('.hideDivs').hide(); 
        $('.formdrop').hide();
        $('.block').html2canvas({
            onrendered: function (canvas) {
                 //Set hidden field value to image data (base-64 string)
                $('#img_val').val(canvas.toDataURL("image/jpg"));
                 $('#img_val3').val($('.block').html()); //html of div
                //Submit the form manualy

            }
        });

3. PHP back end with TC PDF
if(isset($_REQUEST['img_val'])):
        //Get the base-64 string from data
        $filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
        //Decode the string
        $unencodedData=base64_decode($filteredData);
        //Save the image
        $imgfile=FCPATH.'images/submsnimage_'.$this->session->userdata('admin_id').'.jpg';
        $imagegerated=file_put_contents($imgfile, $unencodedData);
        chmod($imgfile, 0777);
        $pdfImageFile=site_url()."images/submsnimage_".$this->session->userdata('admin_id').'.jpg';
endif;

4 PDF generation
        ini_set('max_execution_time',300);
        require_once (FCPATH.'tcpdf/config/lang/eng.php');
        require_once (FCPATH.'tcpdf/tcpdf.php');
        $exa = new TCPDF ();
        $exa->SetCreator ( PDF_CREATOR );
        $exa->SetAuthor ( 'Clay County Admin' );
        $exa->SetTitle ( 'Clay County Form' );
        $exa->SetSubject ( 'Example of TCPDF' );
        $exa->SetKeywords ( 'TCPDF, PDF, PHP' );
        $exa->SetFont ( 'times', '', 18 ); 
        $exa->AddPage ();
        $exa->Image ($pdfImageFile, 15, 10, 430,630); 
        $exa->AddPage ();
        $exa->Image ($pdfImageFile2, 15, 10, 430,630); 
        $exa->AddPage ();
        $exa->Image ($pdfImageFile3, 15, 10, 430,630); 

        $exa->setImageScale(PDF_IMAGE_SCALE_RATIO);
        $exa->setJPEGQuality (100);
        $root=site_url();
        $exa->setImageScale(1.53);

        $txt ="";
        $exa->WriteHTML ($txt, true, false, true, false, '');
        $exa->Output (FCPATH.'uploads/pdf/clayCommunityBullyReport.pdf', 'F' ); // downloads the pdf
+1
source

<canvas>, / pdf. <canvas>, . , : toDataURL(), , /, , pdf.

php pdf , . FPDF, :

http://www.fpdf.org/

http://www.tcpdf.org/

http://www.php.net/manual/en/book.pdf.php

-4

Source: https://habr.com/ru/post/1755588/


All Articles