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>";
Related
I have a code to convert an html text into pdf and another to merge this pdf with a pdf that the user uploads, but I can't merge the two together, it downloads the converted pdf and not the merged one.
When I put just to merge with two files that the user uploads it works.
My code:
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$arquivo = $dompdf->stream();
$ilovepdf = new Ilovepdf('iLovePdfKey', 'iLovePdfKey');
// Create a new task
$myTaskMerge = $ilovepdf->newTask('merge');
// Add files to task for upload
$arquivo = $this->convertHello();
$file1 = $myTaskMerge->addFile('path to the file that the user upload');
$file2 = $myTaskMerge->addFile($arquivo);
// Execute the task
$myTaskMerge->execute();
// Download the package files
$myTaskMerge->download();
$dompdf->stream() sends the rendered PDF to the browser. As such you can't access the generated PDF that way. You have to capture the output and save to a file.
Based on your sample code, something like this:
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
$dompdf->render();
$arquivo = $dompdf->output();
$tmp = tempnam(sys_get_temp_dir(), "pdf")
file_put_contents($tmp, $arquivo);
$ilovepdf = new Ilovepdf('iLovePdfKey', 'iLovePdfKey');
$myTaskMerge = $ilovepdf->newTask('merge');
$file1 = $myTaskMerge->addFile('path to the file that the user upload');
$file2 = $myTaskMerge->addFile($tmp);
$myTaskMerge->execute();
unset($tmp);
$myTaskMerge->download();
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();