I am having an issue with generating pdf documents. My tabulator characters are not displayed properly. I already tried different fonts, or encodings (UTF-8, Windows1552).
With some fonts the character is completely hidden. With some I get a square symbol displayed instead of my tab "\t".
Here is my code.
The question is "How to display tabs using Zend_PDF?"
public function generate()
{
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page( Zend_Pdf_Page::SIZE_A4 );
//render basic template
$template = Zend_Pdf_Image::imageWithPath( APPLICATION_PATH . '/resources/pdf/template.png' );
$page->drawImage( $template, 0 ,0, 595, 842 );
//render document title
$font = Zend_Pdf_Font::fontWithPath( APPLICATION_PATH . '/resources/pdf/arial-bold.ttf' );
$page ->setFont($font, 14)
->drawText( 'Rechnung', 390, 700, 'utf-8' );
//render reciever adress
$font = Zend_Pdf_Font::fontWithPath( APPLICATION_PATH . '/resources/pdf/arial.ttf' );
$adressText = array(
'Kundennummer' . "\t" . $this->_user->getUserIdString(),
'Belegnummer' . "\t" . $this->_payin->getPayinIdString(),
'Datum' . "\t\t\t" . $this->_payin->getDateCreated()->format( 'd.m.Y' ),
'Seite' . "\t\t\t" . '1/1'
);
$page ->setFont($font, 12);
$adressY = 680;
foreach( $adressText as $line )
{
$page->drawText( $line, 390, $adressY , 'utf-8' );
$adressY -= 12;
}
//add page to pdf document
$pdf->pages[] = $page;
//save pdf
$pdf->save( $this->getOption( 'path' ) );
}
It could be that the pdf doesn't understand \t.
Try replacing it with 'chr(9)' which is the ascii value of the tab character. For example:-
$tab = chr(9);
$adressText = array(
'Kundennummer' . $tab . $this->_user->getUserIdString(),
'Belegnummer' . $tab . $this->_payin->getPayinIdString(),
// etc..
);
Correction:
As you have to supply the x,y coordinates to Zend_Pdf_Page::drawText() things like tabs, line feeds, etc will not work.
You will have to set fixed coordinates for your tab stops.
For example:-
$tabs = array(5, 20, 30, 50);
$page->drawText("At 1st tab", $tabs[0], 10);
$page->drawText("At 2nd Tab", $tabs[1], 10);
$page->drawText("At 3rd Tab", $tabs[2], 10);
Hopefully you get the idea.
Related
So I'm working on a reporting application that requires generating Xls files with charts. The charts don't appear to be working though. I've tried just replacing the code below with the example in the PhpSpreadsheet github, but that also doesn't work.
I have the following for inserting the data (and chart) into the worksheet:
$this->spreadsheet->createSheet();
$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->setTitle($title);
$periodStartArray = array(array('', $periodStartDate, $periodEndDate));
while (!empty($stack)) {
$node = array_pop($stack);
if ($node->getData()['portfolioData'][0] != null) {
if ($node->getData()['portfolioData'][0]->getIncludeInStatusSide()) {
$startValue = (float)$this->getDataPoint($node, 'general', $this->periodStart, 'value');
$startValuePercent = number_format(($startValue / $this->startingTotalValues) * 100, $this->percentDecimals, $decimalPoints, $separators);
$endValue = (float)$this->getDataPoint($node, 'general', $this->periodEnd, 'value');
$endValuePercent = number_format(($endValue / $this->endingTotalValues) * 100, $this->percentDecimals, $decimalPoints, $separators);
$startArray = array($node->getData()['portfolioData'][0]->getNavn(), $startValuePercent, $endValuePercent);
$periodStartArray[] = $startArray;
}
}
}
$maxSize = count($periodStartArray);
$worksheet->fromArray($periodStartArray);
//$title . '!$B$1'
$dataSeriesLabels1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $title . '!$B$1', null, 1),
];
// $maxSize - 1 should give the actual number of data points (since the first entry in the array is the
// date row
$dataString = "'" . $title . "'" . '!$A$2:$A$' . $maxSize;
$xAxisTickValues1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $dataString, null, $maxSize - 1)
];
$dataString = "'" . $title . "'" . '!$B$2:$B$' . $maxSize;
$dataSeriesValues1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $dataString, null, $maxSize - 1)
];
$series = new DataSeries(
DataSeries::TYPE_PIECHART,
null,
range(0, count($dataSeriesValues1) - 1),
$dataSeriesLabels1,
$xAxisTickValues1,
$dataSeriesValues1
);
$layout = new Layout();
$layout->setShowVal(true);
$layout->setShowPercent(false);
$plotArea = new PlotArea($layout, array($series));
$legend = new Legend(Legend::POSITION_RIGHT, null, false);
$title = new Title($title);
$chart1 = new Chart(
'chart1',
$title,
$legend,
$plotArea,
true,
0,
null,
null
);
$chart1->setTopLeftPosition('A10');
$chart1->setBottomRightPosition('P25');
$worksheet->addChart($chart1);
It successfully inserts the data into the spreadsheet, but does not print out any charts. Here is where I'm saving the spreadsheet:
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($this->spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($this->outputFile);
So I'm including the charts, but no charts appear. When I change the format to Xlsx, it seems to at least try to insert the charts but they're corrupt and Excel removes them. It's just simple data. Literally one row of a company name in the A column and two dates of values down the B and C columns. When I manually insert the charts after the excel file gets generated it works no problem.
use this
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
instaea of 0
like in sample file
https://github.com/PHPOffice/PhpSpreadsheet/blob/master/samples/templates/chartSpreadsheet.php
Change the 0 to 'gap' when creating the chart.
$chart1 = new Chart(
'chart1',
$title,
$legend,
$plotArea,
true,
'gap', // use 'gap' instead of 0,
null,
null
);
Below is the issue for PhPSpreadsheet
File containing a chart can not be opened by Excel 2003/2013/2019
I have a website made by previous developed( Now he is not available ).
I have to access to CMS, payment backend everything, But old developer didnt left any records of database.
I am trying to rename or replace some of the text(company details) in receipt, but I dont know what am i doing wrong.
If I had an access to DB , it would be ease like butter.
Here is what I want to do :
You can see that I tried to include direct text "www.transit.com" , but it didnt show up on receipt .
Instead it showed blank.
I know its pointing to DB name like 'shop_name' and so on. But since I dont have any details on DB , I cant change it from DB.
function setShopData() {
// ショップ情報
$objDb = new SC_Helper_DB_Ex();
$arrInfo = $objDb->sfGetBasisData();
// ショップ名
$this->lfText(125, 60, $arrInfo['shop_name'], 8, 'B');
// URL
$this->lfText(125, 63, $arrInfo['www.transit.com'], 8);
// 会社名
$this->lfText(125, 68, $arrInfo['law_company'], 8);
// 郵便番号
$text = '〒 ' . $arrInfo['law_zip01'] . ' - ' . $arrInfo['law_zip02'];
$this->lfText(125, 71, $text, 8);
// 都道府県+所在地
$text = $this->arrPref[$arrInfo['law_pref']] . $arrInfo['law_addr01'];
$this->lfText(125, 74, $text, 8);
$this->lfText(125, 77, $arrInfo['law_addr02'], 8);
$text = 'TEL: '.$arrInfo['law_tel01'].'-'.$arrInfo['law_tel02'].'-'.$arrInfo['law_tel03'];
//FAX番号が存在する場合、表示する
if (strlen($arrInfo['law_fax01']) > 0) {
$text .= ' FAX: '.$arrInfo['law_fax01'].'-'.$arrInfo['law_fax02'].'-'.$arrInfo['law_fax03'];
}
$this->lfText(125, 80, $text, 8); //TEL・FAX
if (strlen($arrInfo['media-support.transit-grp.com']) > 0) {
$text = 'Email: '.'media-support.transit-grp.com';
$this->lfText(125, 83, $text, 8); //Email
}
So is there anyway I can replace these code, so that it would reflect on receipt?
Thank you
Try:
$this->lfText(125, 63, 'www.transit.com', 8);
You were referring to your array, but it didn't contain that key, you simply wanted to use a hardcoded string instead.
For the email (for instance) do:
$text = 'Email: mycompany#gmail.com';
$this->lfText(125, 83, $text, 8);
I suppose the best option is to replace the value in the array, that way any other places where this name is used will also be replaced.
function setShopData() {
// ショップ情報
// Here is the change:
$arrInfo['shop_name'] = "something else";
$objDb = new SC_Helper_DB_Ex();
$arrInfo = $objDb->sfGetBasisData();
// ショップ名
$this->lfText(125, 60, $arrInfo['shop_name'], 8, 'B');
// URL
$this->lfText(125, 63, $arrInfo['www.transit.com'], 8);
// 会社名
$this->lfText(125, 68, $arrInfo['law_company'], 8);
// 郵便番号
$text = '〒 ' . $arrInfo['law_zip01'] . ' - ' . $arrInfo['law_zip02'];
$this->lfText(125, 71, $text, 8);
// 都道府県+所在地
$text = $this->arrPref[$arrInfo['law_pref']] . $arrInfo['law_addr01'];
$this->lfText(125, 74, $text, 8);
$this->lfText(125, 77, $arrInfo['law_addr02'], 8);
$text = 'TEL: '.$arrInfo['law_tel01'].'-'.$arrInfo['law_tel02'].'-'.$arrInfo['law_tel03'];
//FAX番号が存在する場合、表示する
if (strlen($arrInfo['law_fax01']) > 0) {
$text .= ' FAX: '.$arrInfo['law_fax01'].'-'.$arrInfo['law_fax02'].'-'.$arrInfo['law_fax03'];
}
$this->lfText(125, 80, $text, 8); //TEL・FAX
if (strlen($arrInfo['law_email']) > 0) {
$text = 'Email: '.$arrInfo['law_email'];
$this->lfText(125, 83, $text, 8); //Email
}
One thing that I find odd is function setShopData() { that is usually function setShopData($arrInfo) {.
Since that is missing I assume the array is global and is set somewhere else.
At some point in the code there is a line like:
$arrInfo['shop_name'] =<database value>
If you place the line above in the code just below this any changes in the rest of the code will have the new shopname everywhere, not just on the receipt.
I am trying to set up a labeling printing system in PHP and on each label I would like to add a logo on top of it and I can't manage to work it, because on fpdf images are treated as a cell it doesn't work
EDIT:
The code is as follows:
for($i=1;$i<=$nolabels;$i++) {
$text = sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s%s%s", "", "","","", "$destination", "$label", "USE BY: $date", "$i", '/', "$nolabels");
$pdf->Add_Label($text);}
for($i=1;$i<=$nolabels2;$i++) {
$text = sprintf("%s\n%s\n%s\n%s%s%s", "$destination", "$label2", "USE BY: $date", "$i", '/', "$nolabels2");
$pdf->Add_Label($text);
You can make additional "addHeader" and call it on each page. Here is one example with using X and Y coordinates
public function addHeader($pdf, $addPageNum)
{
// Header
if ($addPageNum) {
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(168, 168, 168);
$pdf->SetXY(20, 265);
$pdf->Cell(0, 5, __('Page') . ' ' . $pdf->PageNo() . ' von ' . '{nb}', 0, 1, 'C', 0);
}
// Footer photo
$pdf->Image(ROOT . '/webroot/img/companyLogo.png', 0, 270, 210);
}
I have generated a PDF using mpdf library in CodeIgniter. I want to attach header image and footer image with proper margin, I have created one code but header and footer get overlapped.
//controller
$this->load->library('m_pdf');
$param = '"","A4","","",0,0,100,0,6,3,"L"';
$pdf = $this->m_pdf->load($param);
// $pdf->useOddEven = true;
$pdf->SetHTMLHeader('<img src="' . base_url() . 'custom/Hederinvoice.jpg"/>');
$pdf->SetHTMLFooter('<img src="' . base_url() . 'custom/footarinvoice.jpg"/>');
$wm = base_url() . 'custom/Watermark.jpg';
$pdf->SetWatermarkImage($wm);
$pdf->showWatermarkImage = true;
$data['main_content'] = 'dwnld';
$this->load->view('template', $data);
$html = $this->load->view('template_pdf', $data, true);
$this->load->view('template_pdf', $data, true);
$pdf->WriteHTML($html);
$pdf->page = 0;
$pdf->state = 0;
$pdf->Output($pdfFilePath, "D");
$pdf->SetHTMLHeader('<img src="' . base_url() . 'custom/Hederinvoice.jpg"/>');
$pdf->SetHTMLFooter('<img src="' . base_url() . 'custom/footarinvoice.jpg"/>');
$wm = base_url() . 'custom/Watermark.png';
$data['main_content'] = 'dwnld';
//$this->load->view('template', $data);
$html = $this->load->view('template_pdf', $data, true);
$this->load->view('template_pdf', $data, true);
$pdf->AddPage('', // L - landscape, P - portrait
'', '', '', '',
5, // margin_left
5, // margin right
60, // margin top
30, // margin bottom
0, // margin header
0); // margin footer
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, "D");
Addpage for to creating page in mpdf and pass parameters of margin top,bottom then we get proper output
You can set custom HTMl Header for first page and Other page as below:
Just add the below snippet above all html content. You can add Inline CSS for it using tags above al html
<htmlpageheader name="firstpage" style="display:none">
<div style="text-align:center"><img src="'.'logo.png"/></div>
</htmlpageheader><sethtmlpageheader name="firstpage" value="on" show-this-page="1" />
<htmlpageheader name="otherpages" style="display:none">
<div style="text-align:center">{PAGENO}</div>
</htmlpageheader><sethtmlpageheader name="otherpages" value="off" />';
Or can use https://mpdf.github.io/headers-footers/headers-footers.html as refrence to use HTML header function
To Set footer one can use:
// Footer Configuration for all pages, can be changed for odd and even pages accordingly
$oddEvenConfiguration =
[
'L' => [ // L for Left part of the header
'content' => 'Your Content',
'font-size' => 10,
'font-family' => 'sans-serif',
'color'=>'#000000'
],
'C' => [ // C for Center part of the header
'content' => '',
],
'R' => [
'content' => '{PAGENO}',
],
'line' => 0, // That's the relevant parameter to remove line style in footer
];
$headerFooterConfiguration = [
'odd' => $oddEvenConfiguration,
'even' => $oddEvenConfiguration
];
I am trying to create 2 separate pdf files with different content. I have the following code which creates the 2 files.
$invoice = Invoices::model()->findByPk($_GET['id']);
$invoicedetail = InvoicesDetail::model()->findAll(array("condition"=>"invoiceno=".$invoice->INVOICENO . " && biditems_id is null"));
$client = $invoice->pROJ->oRDERNO->addressesinvoices;
$invoices = Invoices::model()->findAll(array("condition"=>"PROJID=".$invoice->PROJID . " && INVOICENO != ".$invoice->INVOICENO));
/*$this->renderPartial("partialInvoice",
array(
"invoice"=>$invoice,
"client"=>$client,
//"lookupprocesstotalArr"=>$lookupprocesstotalArr,
"adjustments"=>$invoices,
"invoicedetail"=>$invoicedetail[0]
),true);
*/
$html = $this->renderPartial("partialInvoice",
array(
"invoice"=>$invoice,
"client"=>$client,
//"lookupprocesstotalArr"=>$lookupprocesstotalArr,
"adjustments"=>$invoices,
"invoicedetail"=>$invoicedetail[0]),true);
$filename_invoice = 'invoice_'.$invoice->INVOICENO.'.pdf';
Yii::import('application.extensions.pdfable.WkHtmlToPdf');
$pdf = new WkHtmlToPdf(array(
'no-outline', // Make Chrome not complain
'margin-top' => 10,
'margin-right' => 10,
'margin-bottom' => 10,
'margin-left' => 10,
));
$pdf->setOptions(array(
'orientation' => 'portrait'
));
// Add a HTML file, a HTML string or a page from a URL
$pdf->addPage($html);
// Save the PDF
$pdf->saveAs("/tmp/$filename_invoice");
// ... or send to client as file download
/*if(!$pdf->send($filename)){
throw new Exception('Could not create PDF: '.$pdf->getError());
}*/
$globalController = new GlobalController;
//$project = Projects::model()->findAll(array("condition"=>"PROJID=".$_GET['PROJID']));
//$projid = $_GET['PROJID'];
$pdfDetail = new WkHtmlToPdf();
$orientation = "Landscape";
$pdfDetail->setOptions(array('orientation'=>$orientation,
'no-outline', // Make Chrome not complain
'margin-right' => 10,
'margin-bottom' => 5,
'margin-left' => 10,
));
//$this->layout = 'pdf';
//get list of process stages for this project
$lastrun = $globalController->lastRun($invoice->PROJID);
$processstages = ExtHtml::listData($lastrun, 'ID',array('cumulative','ITEM'));
$lookupprocesstotalArr = $globalController->lookupprocesstotal($invoice->PROJID,$processstages);
// get all lines
$connection = Yii::app()->db;
$sql = "SELECT jobs.JOBNO, jobs.NAME, FSP, LSP, SHOTS, KM
FROM hdb.jobs
join detailsseismic on jobs.JOBNO = detailsseismic.JOBNO
where jobs.PROJID = :pid";
$command=$connection->createCommand($sql);
$command->bindValue(":pid",$invoice->PROJID,PDO::PARAM_INT);
$command->execute();
$lines = $command->queryAll();
$dates = array();
foreach ($lines as $arr) {
$sql = "SELECT jobsprocesscomplete.datedone, ITEM_fk
FROM hdb.jobsprocesscomplete
left join lookupprocess on jobsprocesscomplete.lookupprocess_id = lookupprocess.ID
left join biditems on lookupprocess.biditems_id = biditems.ITEMID
where jobno = :jobno";
$command=$connection->createCommand($sql);
$command->bindValue(":jobno",$arr['JOBNO'],PDO::PARAM_INT);
$command->execute();
$dates[$arr['JOBNO']] = $command->queryAll();
}
/*echo $this->renderPartial('pdf_'.$_GET['type'],array(
'lookupprocesstotalArr'=>$lookupprocesstotalArr,
"currency"=>$project[0]->currency0->currency,
"projectpercent"=>$project[0]->PERCENT,
"name"=>$project[0]->PROJECT . " - " . $project[0]->oRDERNO->cONTACTsugar->first_name . " " . $project[0]->oRDERNO->cONTACTsugar->last_name,
"lines"=>$lines,
"dates"=>$dates,
"project"=>$project[0]));
*/
$detail = $this->renderPartial('/lookupprocess/pdf_detail',array(
'lookupprocesstotalArr'=>$lookupprocesstotalArr,
"currency"=>$invoice->pROJ->currency0->currency,
"projectpercent"=>$invoice->pROJ->PERCENT,
"name"=>$invoice->pROJ->PROJECT . " - " . $invoice->pROJ->oRDERNO->cONTACTsugar->first_name . " " . $invoice->pROJ->oRDERNO->cONTACTsugar->last_name,
"lines"=>$lines,
"dates"=>$dates,
"project"=>$invoice->pROJ
),true);
$filename_detail = $invoice->PROJID . "_" . date("ymd-his") . ".pdf";
// Add a HTML file, a HTML string or a page from a URL
$pdfDetail->addPage($detail);
// Save the PDF
$pdfDetail->saveAs("/tmp/$filename_detail");
shell_exec("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/tmp/$filename_invoice /tmp/$filename_invoice /tmp/$filename_detail");
echo "gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/tmp/$filename_invoice /tmp/$filename_invoice /tmp/$filename_detail";
exit();
/*
// ... or send to client as file download
if(!$pdf->send($filename_invoice)){
throw new Exception('Could not create PDF: '.$pdf->getError());
}
The problem i have is the 1st pdf that gets created is overwritten by the content of the 2nd pdf which makes no sense.
I do not know the class you are using but this code seems suspicios:
-sOutputFile=/tmp/$filename_invoice /tmp/$filename_invoice /tmp/$filename_detail
For me it seems that you convert 2 files into 1 output files, so logically you overwrite it!?