Include stylesheet file into MPDF conversion in PHP - php

I have made HTML to convert it into PDF using MPDF,
but issue is that I can't include a stylesheet file.
Example:
include("../mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyleA4.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
I used the code above but I can't see the effect of the styles.
Does anyone know a better way to include styles?

You need to load the stylesheet in the html - it's not clear in your snippet where the content for $html comes from but that's the place to load styles. Using your code you'll be writing the CSS you've loaded as if it were document content for the PDF, which I assume isn't your intent.

mPdf works best with inline style sheets which cause no bugs while pdf opens in different browsers.

It looks to me you are setting the new PDF function into "Code" mode.
$mpdf=new mPDF('c');
Have you tried it without the "C"?
$mpdf=new mPDF();
Also, is your stylesheet, "mpdfstyleA4.css", located in the same path as mpdf.php?

Related

dompdf not rendering properly on display:inline-block

Good Day,
I am trying to render a calendar to pdf using dompdf,
heres my html
and my php
<?php
// require .'vendor/dompdf/autoload.inc.php';
require("vendor/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// Load HTML content
$dompdf->loadHtml($_REQUEST['html']);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('LETTER', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>
the html is is good I think, just without head and html tag.
and heres the result.
can you please help me figure what wrong? the html displays fine on browser too.
Might be too late to this answer, but anyone looking for this issue, please try display: inline, it solves this issue but still need to specify width and height and not auto calculate.

create pdf file of page with dynamic content

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

PHP: Adobe Reader can't open PDF files created with mpdf

I'm using mpdf to create PDF files on the fly, and the files open fine in a browser but Adobe gives me an error:
Adobe Acrobat Reader DC could not open 'example-filename.pdf'
because it is either not a supported file type or because the file
has been damaged (for example, it was sent as an email attachment
and wasn't correctly decoded).
I looked at other questions about this (another mpdf + adobe error), and checked out the pdf in a text editor. I found that the first part of the file looked like this:
<!DOCTYPE html>
<head>
<title>
CapstoneDB
</title>
%PDF-1.4
%âãÏÓ
After I removed everything up to %PDF-1.4 (including the tab), the file opened fine in Adobe, which is great, except that I need to be able to get the generated pdfs to open in Adobe without manually fiddling with the code every time.
Here's my wrapper function that calls mpdf with the html and css:
include('../mpdf/mpdf.php');
function user_download_pdf($html, $css_file, $filename) {
$mpdf = new mPDF();
$stylesheet = file_get_contents($css_file);
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output($filename, "D");
}
I never feed mpdf a full html page, usually just an h3 and one or more tables. Maybe I need to be giving mpdf an entire html page, including <head>, <body>, etc? Is there any way to change mpdf configuration or the way I call mpdf in php that would get rid of the html junk at the beginning of the pdf file that's gunking everything up?
Place
ob_clean();
immediately before
$mpdf->Output();
Without this mpdf sometimes includes the website page HTML and not just the HTML that you want in the PDF, probably because headers have already been sent elsewhere in the code. That can mess up your PDF so Adobe won't open it.

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

how to include graphs into html2pdf?

I am using maxChart.class.php to create Graphs and its working properly.
I am also using html2pdf.class.php to create Online pdf report. I tried to include chart in pdf file but its not working.
My code is
$rowval=$sqcat1->qcategory;
$data3["$rowval"]=$per;
$content = "
<page>
$content.=$mc3 = new maxChart($data3);
$mc3->displayChart('',1,450,400,true);
"
</page>";
require_once("/pdf1/html2pdf.class.php");
$html2pdf = new HTML2PDF('P','A4','en');
$html2pdf->WriteHTML($content);
$html2pdf->Output('demo.pdf');
How can i add graphs in file ?
Anybody please help me.
I used MPDF and SVGGraphs to do this
Code to add graphs value in php variable
$html.='<br>'.$graph->Fetch("CylinderGraph",true).'<br>';
Code for MPDF
include("/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetHTMLHeader($header);
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
You can do the following:
make the chart an image and embed that image in the document, using <img src=...
display the chart image using binary data <img src="data:image/png;base64,binarycontentherebase64encoded...">

Categories