How to generate multiple copies of same PDF file in dompdf - php

I am using dompdf to generate invoices, one invoice is generated but I want to generate 3 copies of same invoice pdf file which is copy1, copy2, copy3. Each copy should start on new page of the same pdf file.
$dompdf = PDF::loadView('admin.pdf.invoice', compact('data'));
$options = [];
$options['isHtml5ParserEnabled'] = true;
$options['isRemoteEnabled'] = true;
$options['isPhpEnabled'] = true;
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
$file =LOCAL_PDF_PATH."invoice.pdf";
$dompdf->save(LOCAL_PDF_PATH."invoice.pdf");

Try this code
for($i=0;$i<3;$i++){
$dompdf = PDF::loadView('admin.pdf.invoice', compact('data'));
$options = [];
$options['isHtml5ParserEnabled'] = true;
$options['isRemoteEnabled'] = true;
$options['isPhpEnabled'] = true;
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
$file =LOCAL_PDF_PATH."invoice.pdf";
$dompdf->save(LOCAL_PDF_PATH."invoice".$i.".pdf");
}

Use array chunk for that inside your view
$columns = 3; // number of columns on a page
// calculate number of rows and break the data into chunks
$num_rows = ceil(count($data) / $columns);
$data = array_chunk($data,$num_rows);

Related

PHP webscraper does not produce errors nor start the loop/create output

I am writing a web scraper in PHP using gitpod. After a while I have managed to solve all problems. But even though no problems are left, the code does not open the browser nor produce any output.
Does anybody have an idea why that could be the case?
<?php
if (file_exists('vendor/autoload.php')) {
require 'vendor/autoload.php';
}
use Goutte\Client;
$client = new Goutte\Client();
// Create a new array to store the scraped data
$data = array();
// Loop through the pages
if ($client->getResponse()->getStatus() != 200) {
echo 'Failed to access website. Exiting script.';
exit();
}
for ($i = 0; $i < 3; $i++) {
// Make a request to the website
$crawler = $client->request('GET', 'https://ec.europa.eu/info/law/better-regulation/have-your-say/initiatives_de?page=' . $i);
// Find all the initiatives on the page
$crawler->filter('.initiative')->each(function ($node) use (&$data) {
// Extract the information for each initiative
$title = $node->filter('h3')->text();
$link = $node->filter('a')->attr('href');
$description = $node->filter('p')->text();
$deadline = $node->filter('time')->attr('datetime');
// Append the data for the initiative to the data array
$data[] = array($title, $link, $description, $deadline);
});
// Sleep for a random amount of time between 5 and 10 seconds
$sleep = rand(5,10);
sleep($sleep);
}
// Open the output file
$fp = fopen('initiatives.csv', 'w');
// Write the header row
fputcsv($fp, array('Title', 'Link', 'Description', 'Deadline'));
// Write the data rows
foreach ($data as $row) {
fputcsv($fp, $row);
}
// Close the output file
fclose($fp);
?>

"/n" show after return line in pdf

I am trying to generate a pdf file via the dompdf library (the displayed values ​​come from a mysql database), and I have an encoding problem. I did a lot of research but without success.
The problem is that at each line break, a "/ n" is displayed.
I tried to use different functions html_entities_decode, nl2br and even str_replace ("/ n", "", $ str), but could not delete it.
The "/ n" is not registered in the database at the specified value. I looked in the source files dompdf and it does not come from there either.
A big thank you in advance for your responses !
CODE
use Barryvdh\DomPDF\Facade as PDF; // Barryvdh use dompdf
// function used
public function downloads($from, $type, $unique_id, $downloads, $dom, $do) {
$data['from'] = $from;
$data['type'] = $type;
$data['unique_id'] = $unique_id;
$data['title'] = 'PDF Generation';
//User Details
$userController = new userController;
$data['logeduser'] = $userController->logeduser();
$userpermissions = $userController->getpermissions();
//Notification Details
$notification = new notificationModel;
$data['notification_list'] = $notification->getAllRemainder();
//PDF Generation
$sales = new salesModel();
$data['offer_sav'] = $sales->getOfferSAVDetails($unique_id);
$data['offer_sav_products'] = $sales->getOfferSAVProductDetails($unique_id);
$data['offer_eg'] = $sales->getOfferSAVDetails($unique_id);
$data['offer_eg_component'] = $sales->getOfferEGComponent($unique_id);
$data['offer_eg_accessory'] = $sales->getOfferEGAccessory($unique_id);
$data['offer_eg_options'] = $sales->getOfferEGOptions($unique_id);
$data['sav_eg_without_sm'] = $sales->getSAVEGWithoutSM($unique_id);
//view()->share($data);
//return view('sales/pdfonly', $data);
$pdf = PDF::loadView('sales/pdfonly', $data);
//$view = view('sales/pdfonly', $data);
//$pdf = PDF::loadHTML($view);
if(!empty($_GET['print']))
return $pdf->stream('pdf_'.$from.'_'.$type.'.pdf', array("Attachment" => 0));
else
return $pdf->download('pdf_'.$from.'_'.$type.'.pdf');
}
// exemple of code output
{!! $eg->sales_offer_note2 !!} // = remarque1 <div>r<br> remarque2</div><div><br></div><div><br></div><div>
€
/*
OUTPUT
remarque1
r/n
emarque2
/n
/n
€
/n
*/

How to use mPDF in YII, when I want to generate PDF file from HTML?

$mPDF1 = Yii::app()->ePdf->mpdf();
$mPDF1 = Yii::app()->ePdf->mpdf('', 'A4');
$html= $this->renderPartial('poPdfTemplate', array('pdfValues'=>$ponumberData,'po_number'=>$po_number), true);
$mPDF1->WriteHTML($html,0);
//$mPDF1->WriteHTML($html);
$mPDF1->Output();
The above code is using for generating PDF with YII framework.
First two lines will create css $pdf_style and html file $pdf_part and PDF will be write this file.
Include these 2 variables in the WriteHTML function.
$pdf_style = \Yii::$app->view->renderFile(PROTECTEDPATH . 'common/views/contract/style.css', $data);
$pdf_part = \Yii::$app->view->renderFile(PROTECTEDPATH . 'common/views/contract/file.tpl', $data);
$mpdf = new \mPDF('utf-8','A4');
$mpdf->autoScriptToLang = true;
$mpdf->baseScript = 1; // Use values in classes/ucdn.php 1 = LATIN
$mpdf->autoVietnamese = true;
$mpdf->autoArabic = true;
$mpdf->autoLangToFont = true;
$mpdf->SetMargins(null,null,40);
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetHTMLHeader('<div style="text-align:right"><img src="'. DOCROOT .'images/logo_contract.png" height="68" /></div>');
$mpdf->setFooter(' | www.facebook.com | {PAGENO}');
$mpdf->WriteHTML($pdf_style.'<div class="wrapper">'.$pdf_part.'</div>');
$mpdf->Output('Contract-'.date("iYsmd"), $action, $dest);

WriteHtml Can't Work Properly in mpdf send mail with attachment in php mvc?

I tried to make pdf from html from mpdf library and send mail with pdf file attachment.but i can't. WriteHtml Can't Responced.
WriteHtml Give Response :
Headers no-store no-chach type:html/text
$mpdf = $StockModel->getPdfObj();
$mpdf->allow_charset_conversion = true;
$mpdf->charset_in = 'ISO-8859-2';
//$mpdf->allow_html_optional_endtags = true;
//$mpdf->restoreBlockPageBreaks = true;
$mpdf->useOnlyCoreFonts = true;
//$mpdf->SetProtection(array('print'));
$mpdf->SetHeader('|'.$stype.'|Date '.date("d-M-Y").' ');
$mpdf->SetFooter('www.angellogistic.in');
$mpdf->defaultheaderfontsize=7;
$mpdf->defaultheaderfontstyle='B';
$mpdf->defaultheaderline=0;
$mpdf->defaultfooterfontsize=7;
$mpdf->defaultfooterfontstyle='BI';
$mpdf->defaultfooterline=0;
$mpdf->SetTitle("angel logistic");
$mpdf->SetAuthor("angel logistic");
$mpdf->SetWatermarkText("angel logistic");
$mpdf->showWatermarkText = true;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML(utf8_encode($html));
In html Assign html and inline css code and tags.

Cyrillic text with dompdf, print pdf scale error

I need a help with this one. I use PHP and dompdf to create this invoice in Cyrillic:
On pdf everything is looking fine, but when I try to print it, I get it like:
and I get also one extra blank page added. Do you know how this can be fixed?
The example can be seen at: http://projects.stanislavstankov.com/php-pdf/pdfview.php
and sourse downloaded at: http://projects.stanislavstankov.com/php-pdf/php-pdf.rar
PHP code:
<?php
$data = file_get_contents("HTML Invoice Template.html");
$data = iconv('UTF-8//IGNORE','UTF-8//IGNORE', $data);
$data = preg_replace('/<body>/', "", $data);
$data = preg_replace('/<\/body>/', "", $data);
$data = preg_replace('/<html>/', "", $data);
$data = preg_replace('/<\/html>/', "", $data);
// inhibit DOMPDF's auto-loader
define('DOMPDF_ENABLE_AUTOLOAD', false);
//include the DOMPDF config file (required)
require 'extensions/dompdf/dompdf_config.inc.php';
//if you get errors about missing classes please also add:
require_once('extensions/dompdf/include/autoload.inc.php');
//$data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
//$data = utf8_decode($data);
//$data = iconv('Windows-1251','UTF-8', $data);
//
//generate some PDFs!
$dompdf = new DOMPDF(); //if you use namespaces you may use new \DOMPDF()
$dompdf->set_paper('a4', 'portrait');
$dompdf->load_html($data);
//$dompdf->set_paper(array(0,0,595,842));
$dompdf->render();
$dompdf->stream($log_id.".pdf", array("Attachment"=>0));
?>

Categories