create pdf file of page with dynamic content - php

I need to create a PDF file of a php page that includes php variables and js generated content.
I tried achieving this with mPDF and the following script:
require_once "libraries/mpdf/mpdf.php";
ob_start(); // start output buffering
include 'appartamento.php';
$content = ob_get_clean(); // get content of the buffer and clean the buffer
$mpdf = new mPDF();
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($content);
$mpdf->Output(); // output as inline content
However, the generated pdf does only include very few content of the php page and i don't know what needs to be done to display the whole content in the pdf file.
Any ideas?

To generate the dynamic content, treat your $content variable is any PHP variable and set it initially.
After that, where you are populating the content in your loop, ensure to use the below logic.
$content = $content . "Your additional Content";
In this way, you will append to your variable.
Finally, use the below code to generate the PDF.
$mpdf->WriteHTML($content);
$mpdf->Output();
exit;
You can easily use almost all the HTML tags in your $content. There are only some tags not supported.
That's it!
I have added the step-by-step process to ensure that you generate the PDF successfully with the dynamic data.
$mpdf=new mPDF('win-1252','A4','','',20,15,55,25,10,10); //This is where you set page size and margins. Feel free to use as is.
$mpdf->useOnlyCoreFonts = true; // false is default.
$mpdf->SetProtection(array('print')); //You can find out about these details in mPDF guidelines.
$mpdf->AliasNbPages(['PAGETOTAL']); // PAGETOTAL gives you the value of Total Pages of your generated PDF.
$mpdf->SetTitle($your_title); //You can even set the PDF Title based on your Data.
$mpdf->SetAuthor($author_name); //You can set Author's Name as well
$mpdf->SetWatermarkText($watermark_text); //You can even set Watermark Text
$mpdf->showWatermarkText = false; //True if you want to show watermark
$mpdf->watermark_font = 'DejaVuSansCondensed'; //Can even change Watermark Font
$mpdf->watermarkTextAlpha = 0.05; //Opacity of Watermark Text
$mpdf->SetDisplayMode('fullpage'); //Obvious as mentioned
$content = $content . 'Your ' . $content . ' can be added this way.'; //This can be any HTML.
$mpdf->WriteHTML($content); //You can now compile your content and show in PDF.
$mpdf->Output(); //finally output it.
In addition to this, please note the following as well.
mPDF is divided into three main sections. These are to be in the <body> of your HTML.
<htmlpageheader name="myheader"> - This is repeated on every page as a header. Can be your logo from the page template. Will close </htmlpageheader>.
<htmlpagefooter name="myfooter"> - This will be similar to the header but at the bottom. Will close </htmlpagefooter>.
This is your entire content in <body>.
To display the <htmlpageheader name="myheader"> and <htmlpagefooter name="myfooter">, use the below lines of code.
<sethtmlpageheader name="myheader" value="on" show-this-page="1" />
<sethtmlpagefooter name="myfooter" value="on" />
Follow this logic and then try.

You could use the fpdf library. It allows you to write a pdf from your PHP.

You should definitely check this out. A very helpful post by the way : https://github.com/dompdf/dompdf
And iff you are new to this, you might want to see how certain examples are rendered eventually : https://dompdf.net/examples.php

Related

How to add meta title to a PDF on the browser tab using dompdf?

I haven't been able to find a way to display on the Browser's tab a Title (like meta title) when generating a PDF suing domppdf.
In some posts they suggest adding this to the HTML code:
<html>
<head><title> My Title </title>
</head>
<body>
/** PDF Content **/
</body>
But, maybe it is due to version that I am using, but adding these tags breaks my code and dompdf returns a long error. I only have the body and it works great.
Unfortunately I can't upgrade my version of dompdf, so I am trying to find another solution.
I don't think it is possible by using PHP headers, but I am open to suggestions.
I have this so far:
$dompdf = new Dompdf();
ob_start();
include("pdf_HTML.php");
$fileName = "download.pdf";
$content = ob_get_clean();
$dompdf->loadHtml($content);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Letter', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
header("Content-type:application/pdf");
header("Content-Disposition: attachment; filename=$fileName.pdf'");
$dompdf->stream($fileName,array('Attachment'=>0));
After much looking I found the way:
$dompdf->add_info('Title', 'Your meta title');
It took me some time to find the answer so I am sure this will be helpful for someone else.

Why MPDF library is always shrinking to single page?

I am using MPDF. My content of the page is dynamic. So even if the content enhances it doesn't make a page break. Rather the content shrinks and font size becomes smaller.
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDisplayMode('default');
$mpdf->WriteHTML($htmldata);
$mpdf->Output($name,"D");
Here $htmldata is my dynamic long html variable. Here's the sample of the pdf
enter link description here
mPDF resizes tables so that their parts fit the page.
See https://mpdf.github.io/troubleshooting/resizing.html
Use <table autosize="0"> to prevent most of this behaviour.

TCPDF breaking my output on PDF

I am using open cart. I am generating a PDF file using TCPDF. I load my html from a tpl file, which has form with dynamic data. here is my code
$pdf = new TCPDF()
$pdf->AddPage('P');
$html = $this->render();
$pdf->writeHTML($html);
$pdf->Output();
Its showing me output but its breaking the result.
I have five parts in my form but its just showing me first two why is this so.
Plus my css is not working well.

Convert php file to pdf file by using mPDF

I have just started to use mPDF. I got stuck at very beginning. I am trying to include my dynamic php file and convert it to pdf file by using mPDF. Here is my approach:This is my function to convert file to pdf
<?php
include('MPDF57/mpdf.php');
include('template1.php');
$html= "template1.php";
$mpdf=new mPDF();
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML(file_get_contents($html));
$mpdf->Output('result.pdf','F');
exit;
?>
My template.php file is only a html invoice table layout in where several content come from database as: user address, invoice table and so on... I do want to convert the html layout and content from the template.php file to pdf
But it is not outputing the file as a pdf file. What am I missing here?
If your template1.php has php code in it then it would not be executed by the file_get_contents function as it will read the content of the file as a regular text. You need to turn output buffer on before include, get the content of the buffer and use it for the generation of pdf. Something like this:
<?php
include 'MPDF57/mpdf.php';
ob_start(); // start output buffering
include 'template1.php';
$content = ob_get_clean(); // get content of the buffer and clean the buffer
$mpdf = new mPDF();
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($content);
$mpdf->Output('result.pdf'); // output as inline content

Adjust Image if page break occurs in mpdf library

I am generating pdf from html source using mpdf library in php and everything seems working perfect.
Now I have an issue with the images. Suppose before page end I'm inserting an image but image is big so that it doesn't fit at the bottom of first page and goes to second page. Now I have a long white space at the end of first page because image moved to second page.
Now I want is "if next item to insert in pdf is an image then calculate the remaining size of pdf page if it is less than Image size then adjust the image size so that image can be fit in the pdf page instead of moving to next page" how can i do this here?
Please check the issue image :
If any one has other solution please help me to sort out.
Here's My sample code
include_once 'simple_html_dom.php'; //import html dom and mpdf library
include 'PDFScript/MPDF/mpdf.php';
$mpdf = new mPDF('','','','',15,15,30,15,8,8); //create mpdf object
$html = new simple_html_dom(); //create html dom object
$html = file_get_html("htmlsource.html"); //htmlsource.html is a webpage can contain any html data
$mpdf->WriteHTML($html); //write html source to pdf
$mpdf->Output(); //generate pdf
I got a solution from mpdf forum itself. If Anyone also has the same problem enclose every image in your html inside table as table has a autosize feature in mpdf library.
For more information please check here

Categories