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));
?>
Related
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);
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
*/
$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);
I have a JSON file badly formatted (doc1.json):
{"text":"xxx","user":{"id":96525997,"name":"ss"},"id":29005752194568192}
{"text":"yyy","user":{"id":32544632,"name":"cc"},"id":29005753951977472}
{...}{...}
And I have to change it in this:
{"u":[
{"text":"xxx","user":{"id":96525997,"name":"ss"},"id":29005752194568192},
{"text":"yyy","user":{"id":32544632,"name":"cc"},"id":29005753951977472},
{...},{...}
]}
Can I do this in a PHP file?
//Get the contents of file
$fileStr = file_get_contents(filelocation);
//Make proper json
$fileStr = str_replace('}{', '},{', $fileStr);
//Create new json
$fileStr = '{"u":[' . $fileStr . ']}';
//Insert the new string into the file
file_put_contents(filelocation, $fileStr);
I would build the data structure you want from the file:
$file_path = '/path/to/file';
$array_from_file = file($file_path);
// set up object container
$obj = new StdClass;
$obj->u = array();
// iterate through lines from file
// load data into object container
foreach($array_from_file as $json) {
$line_obj = json_decode($json);
if(is_null($line_obj)) {
throw new Exception('We have some bad JSON here.');
} else {
$obj->u[] = $line_obj;
}
}
// encode to JSON
$json = json_encode($obj);
// overwrite existing file
// use 'w' mode to truncate file and open for writing
$fh = fopen($file_path, 'w');
// write JSON to file
$bytes_written = fwrite($fh, $json);
fclose($fh);
This assumes each of the JSON object repsentations in your original file are on a separate line.
I prefer this approach over string manipulation, as you can then have built in checks where you are decoding JSON to see if the input is valid JSON format that can be de-serialized. If the script operates successfully, this guarantees that your output will be able to be de-serialized by the caller to the script.
My PHP script successfully reads all text from a .docx file, but I cannot figure out where the line breaks should be so it makes the text bunched up and hard to read (one huge paragraph). I have manually gone over all of the XML files to try and figure it out but I cannot figure it out.
Here are the functions I use to retrieve the file data and return the plain text.
public function read($FilePath)
{
// Save name of the file
parent::SetDocName($FilePath);
$Data = $this->docx2text($FilePath);
$Data = str_replace("<", "<", $Data);
$Data = str_replace(">", ">", $Data);
$Breaks = array("\r\n", "\n", "\r");
$Data = str_replace($Breaks, '<br />', $Data);
$this->Content = $Data;
}
function docx2text($filename) {
return $this->readZippedXML($filename, "word/document.xml");
}
function readZippedXML($archiveFile, $dataFile)
{
// Create new ZIP archive
$zip = new ZipArchive;
// Open received archive file
if (true === $zip->open($archiveFile))
{
// If done, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false)
{
// If found, read it to the string
$data = $zip->getFromIndex($index);
// Close archive file
$zip->close();
// Load XML from a string
// Skip errors and warnings
$xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
$xmldata = $xml->saveXML();
//$xmldata = str_replace("</w:t>", "\r\n", $xmldata);
// Return data without XML formatting tags
return strip_tags($xmldata);
}
$zip->close();
}
// In case of failure return empty string
return "";
}
It is actually quite a simple answer. All you need to do is add this line in readZippedXML():
$xmldata = str_replace("</w:p>", "\r\n", $xmldata);
This is because </w:p> is what word uses to mark the end of a paragraph. E.g.
<w:p>This is a paragraph.</w:p>
<w:p>And a second one.</w:p>
Actually, why don't you use OpenXML? I think it works with PHP too. And then you don't have to go down to the nitty gritty file xml details.
Here is a link:
http://openxmldeveloper.org/articles/4606.aspx