I am using the Mpdf in symfony. I have installed the mpdf through composer like:
composer require mpdf/mpdf
After that require the Mpdf.php in autoload.php.
Then use the code for mpdf is:
$mpdf = new mPDF();
$html = '<p style="color:red;">PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
CSS is not affecting on the HTML. When I'm using the style on tag then it's working fine.
$mpdf = new mPDF();
$html = '<style>p{color:red;}</style><p>PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
When I try to use the CSS with class or ID then also not affecting.
$mpdf = new mPDF();
$html = '<style>p.text-color{color:red;}</style><p class="text-color">PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
You don't need the style tag you can do it as follow:
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
So write your CSS first then your html that was working for me the last time.
https://mpdf.github.io/css-stylesheets/introduction.html
Related
Record.php
<?php
if(isset($_POST['btn'])){
if(!isset($error)){
//create html of the data
ob_start();
?>
<?php
$body = ob_get_clean();
$body = iconv("UTF-8","UTF-8//IGNORE",$body);
include("mpdf/mpdf.php");
$mpdf=new \mPDF('c','A4','','' , 0, 0, 0, 0, 0, 0);
//write html to PDF
$mpdf->WriteHTML($body);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML(file_get_contents('Records.php'));
$mpdf->Output();
}
}
?>
I'm trying to make a pdf file of my current file. It has a table which data is fetching from database. The Problem is when i click on the button it redirects me to the pdf file but it gives me the error and the error is 'Failed to load PDF document'.You can see the error below.What am i doing wrong??
Make things simple:
<?php
include("mpdf/mpdf.php");
echo '<table border="1">';
echo '<tr><td>Put some content here!</td></tr>';
echo '</table>';
$html = ob_get_contents();
$filename = 'filename.pdf';
$mpdf = new mPDF('utf-8', 'A4-P');
$mpdf->WriteHTML($html);
$mpdf->Output($filename,'I');
Don't need to convert charset, this library is already running in UTF-8.
HTML
<p style="color:red;font-size: 30px;">sfsdfsdfdsfsdfdsfdsf</p>sdgfhgfhgfhg
PHP
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
//to put same file html
/*$html1 =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';*/
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//to put other html file
$html = file_get_contents('index.html');
$dompdf->loadHtml($html);
//$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Legal', 'Landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
//$output = $dompdf->output();
//file_put_contents("pdfs/file.pdf", $output);
?>
I am using dompdf to convert my html to pdf, what I want to do here is a Print button in index.html page, when I click on that print button the generated pdf should download to user's system.
How can I achieve that
This shoud work:
<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
if (isset($_GET['action']) && $_GET['action'] == 'download') {
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//to put other html file
$html = file_get_contents('index.html');
$html .= '<style type="text/css">.hideforpdf { display: none; }</style>';
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Legal', 'Landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>1));
}
?>
<a class="hideforpdf" href="generatepdf.php?action=download" target="_blank">Download PDF</a>
Edit: moved the use-part to the top of the code.
Edit 2: added a class to hide the download button.
This code works but the resulting file is being opened like HTML for me not as a pdf or download pdf. How can I fix that?
<?php
require_once("dompdf/dompdf_config.inc.php");
//var_dump("dompdf/dompdf_config.inc.php");
$tiket = $_GET["tiket"];
$file = $this->load->view('admin/report/fm-it-01',$tiket);
$dompdf = new DOMPDF();
//var_dump($dompdf->load_html($file));
$dompdf->load_html_file($file);
$dompdf->render();
$dompdf->stream("sample.pdf"); ?>
Try $dompdf->load_html($file); instead of $dompdf->load_html_file($file);
I'm looking to start with an initial PDF file, one that has graphics and text, and then take some html code which has dynamic values for some user input, generate that as a PDF, hopefully either using the initial PDF as a background, OR somehow running a PHP script afterwards to "merge" both PDF where one acts as a background to another.
I have some code that renders an HTML formatted PDF: (using DOMPDF)
$initialpdf = file_get_contents('file_html.html');
$initialpdf = str_replace(array(
'%replaceText1%',
'%replaceText2%'
), array (
$replaceText1,
$replaceText2,
), $initialpdf);
$fp = fopen('file_html_new.html','w');
file_put_contents('file_html_new.html', $initialpdf);
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
$dompdf = new DOMPDF();
$dompdf->set_paper($paper,$orientation);
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
#file_put_contents($filename . ".pdf", $pdf);
}
$filename = 'HTML_Generated_pdf';
$dompdf = new DOMPDF();
$html = file_get_contents('file_html_new.html');
pdf_create($html,$filename,'Letter','landscape');
The code above takes html file "file_html.html" and does string replacements with user input values, renders this as a new HTML file called "file_html_new.html" and then renders that AS a PDF.
I also have other PHP code that render a PDF by having a PDF as an initial source: (using FPDF)
<?php
ob_clean();
ini_set("session.auto_start", 0);
define('FPDF_FONTPATH','font/');
define('FPDI_FONTPATH','font/');
require('fpdf.php');
require('fpdi.php');
$pdf = new FPDI();
$pdf->setSourceFile("/home/user/public_html/wp-content/myPDF.pdf");
$tplIdx = $pdf->importPage(1);
$specs = $pdf->getTemplateSize($tplIdx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L', 'Letter');
$pdf->useTemplate($tplIdx, 0, 0);
$pdf->SetFont('helvetica');
$pdf->SetXY(30, 30);
$pdf->Write(0, $replaceText1);
ob_end_clean();
$pdf->Output('New_Generated_PDF.pdf', 'F');
?>
This takes an already existing PDF, "myPDF.pdf", and uses it as a background, writing some passed in value to the document, and saving the newly produced document.
While this is essentially what I want to do, I need to work with html because the exact formatting for text gets rigorous and almost impossible to do just by plotting it in manually.
I'm open to using DOMPDF, FPDF, FPDI, TCPDF, or any other PHP resource in order to accomplish this.
Is there a way to fuse the two ways I have above?
For sure you can use different existing PDF documents with FPDI, too. This code should show you the concept (actually I guess that all page formats are A4 portrait):
<?php
$pdf = new FPDI();
// let's get an id for the background template
$pdf->setSourceFile('myPDF.pdf');
$backId = $pdf->importPage(1);
// iterate over all pages of HTML_Generated_pdf.pdf and import them
$pageCount = $pdf->setSourceFile('HTML_Generated_pdf.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// add a page
$pdf->AddPage();
// add the background
$pdf->useTemplate($backId);
// import the content page
$pageId = $pdf->importPage($pageNo);
// add it
$pdf->useTemplate($pageId);
}
$pdf->Output();
I'm using laravel framework in php.
I have Generated the plugin by using this barryvdh/laravel-dompdf
And now my question is how to create the page number for seprate page in pdf like page 1 and page 2 like this in barryvdh/laravel-dompdf ?
<?php
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
For the page count you can use CSS counters.
<html>
<head>
<style>
.page-num:before { content: counter(page); }
</style>
</head>
<body>
<p>Page <span class="page-num"></span></p>
</body>
</html>
Unfortunately dompdf does not yet support the total number of pages using counters (for text similar to "page 1 of 3"). For that you would have to use script.
<?php
require_once("dompdf_config.inc.php");
$html = '...';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->get_canvas()->get_cpdf();
$x = 10; // from left
$y = 10; // from bottom
$text = "Page {PAGE_NUM} of {PAGE_NUM}"; // {PAGE_NUM} and {PAGE_COUNT} are placeholders populated by dompdf
$font = Font_Metrics::get_font("arial", "normal");
$size = "10"; // in pt
$color = array(.3, .3, .3); // rgb, valid values are between 0 and 1
$pdf->page_text($x, $y, $text, $font, $size);
$dompdf->stream("sample.pdf");
?>
Note: the above script snippet is specific to v0.6.x using the CPDF backend.