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.
Related
I generated the multiple pdf from dompdf but how can I save these files locally, because these files only open in the web browser. How can I save them into the server?
my code
include '../../libs/vendor/autoload.php';
include '../../libs/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
if (isset($_POST['submit'])) {
if (isset($_FILES['challan']['tmp_name'])) {
$challan = $_FILES['challan']['tmp_name'];
$spreadsheet = $reader->load($challan);
$sheetData = $spreadsheet->getActiveSheet()->toArray();
if (!empty($sheetData)) {
$html = ob_get_clean();
$html = stripslashes($html);
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
for ($i=0; $i<count($sheetData); $i++) {
$name = $sheetData[$i][0];
$fatherName = $sheetData[$i][1];
$rollNo = $sheetData[$i][2];
$class = $sheetData[$i][3];
$dept = $sheetData[$i][4];
$dompdf = new DOMPDF();
$html .= '<p>Hello World</p>';
$dompdf->load_html($html);
$dompdf->render();
file_put_contents('pdf', $dompdf->output());
$dompdf->stream();
}
}
}
}
I updated the code again, please have a look at it.
You can use the method output() to get the PDF content and save into a file using file_get_contents():
file_put_contents('/path/to/file.pdf', $dompdf->output());
EDIT after question changed :
for ($i=0; $i<count($sheetData); $i++)
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
file_put_contents('pdf' . $i . '.pdf', $dompdf->output());
$dompdf->stream();
}
But it seems strange to generate N times, the same PDF.
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 want to generate multiple pdf files in loop using dompdf. I am using dompdf_0-6-0_beta3. But only one file get generated each time wehn I execute the code. I am providing my code for reference as follows.
include 'dompdf_config.inc.php';
for($i=0; $i<5; $i++)
{
if ( get_magic_quotes_gpc() )
$old_limit = ini_set("memory_limit", "16M");
$dompdf = new DOMPDF();
$dompdf->load_html($i);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
$dompdf->stream($i.".pdf");
}
So please help me in this question.
You'l have to save your all PDFs to server and then redirect User to download these PDFs.
include 'dompdf_config.inc.php';
$file_to_save = '/path/to/your/public_html/pdf/';
for($i=0; $i<5; $i++)
{
if ( get_magic_quotes_gpc() )
$old_limit = ini_set("memory_limit", "16M");
$dompdf = new DOMPDF();
$dompdf->load_html($i);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
//Save PDF in server.
file_put_contents($file_to_save."file".$i.".pdf", $dompdf->output());
}
//open popup window to download all PDFs to client browser.
echo "<script type='text/javascript'>";
for($i=0;$i<5: $i++){
echo "window.open('/pdf/file{$i}.pdf');" ;
}
echo "</script>";