I am using TCPDF to generate PDF file using following command
$pdf->writeHTML($htmlcontent, true, 0, true, 0);
TCPDF also provides a way to create barcode with following commands
$pdf->Cell(0, 0, 'C39+', 0, 1);
$pdf->write1DBarcode('Code 39', 'C39+', '', '', 80, 15, 0.4, $style, 'N');
$pdf->Ln();
I want to be able to write barcode as part of the HTML code above. Is there easy way?
I can potentially call a barcode image inthe writeHTML code above, but not sure how to use above barcode function (or any in TCPDF) which would allow me to create image and then get that image into HTML generation.
You can write TCPDF Methods in HTML as below
<?php
$params = $pdf->serializeTCPDFtagParameters(array('40144399300102444888207482244309', 'C128C', '', '', 0, 0, 0.2, array('position'=>'S', 'border'=>false, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>false, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>2), 'N'));
$str='<table cellspacing="0" cellpadding="1" border="0">
<tr>
<td align="left">barcode</td>
</tr>
<tr>
<td align="center" style="padding-left:5px;">';
$str .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
$str .='</td>
</tr>
</table>';
$pdf->writeHTML($str,true, false,false,false,'left');
$pdf->Output('example_049.pdf', 'I');
?>
For detail reference please check TCPDF example_049.php
TCPDF barcode classes already contains methods to export barcodes in various formats (SVG, PNG and HTML).
2D example:
require_once(dirname(__FILE__).'/2dbarcodes.php');
$barcodeobj = new TCPDF2DBarcode('http://www.tcpdf.org', 'QRCODE,H');
// export as SVG image
//$barcodeobj->getBarcodeSVG(3, 3, 'black');
// export as PNG image
//$barcodeobj->getBarcodePNG(3, 3, array(0,128,0));
// export as HTML code
echo $barcodeobj->getBarcodeHTML(3, 3, 'black');
1D example:
require_once(dirname(__FILE__).'/barcodes.php');
$barcodeobj = new TCPDFBarcode('123456', 'C128');
// export as SVG image
//$barcodeobj->getBarcodeSVG(2, 30, 'black');
// export as PNG image
//$barcodeobj->getBarcodePNG(2, 30, array(0,128,0));
// export as HTML code
echo $barcodeobj->getBarcodeHTML(2, 30, 'black');
Check the documentation and examples at http://www.tcpdf.org for further information.
You could put your barcode number is a fake HTML tag and then parse for that tag as you write out the HTML like in this example.
This would be in your HTML:
some HTML.... <POSTNET>12345-1234</POSTNET> ....some more HTML
This is the code to parse for the fake tag.
// look to see if there is a POSTNET tag
if (strpos($letter_html, "<POSTNET>") !== false) {
$postnet_pre = explode("<POSTNET>", $letter_html);
$this->WriteHTML($postnet_pre[0], $this->line_height);
// write the barcode
$postnet_post = explode("</POSTNET>", $postnet_pre[1]);
$zip_code = $postnet_post[0];
$this->write1DBarcode($zip_code, 'POSTNET', '', '', 80, 15, 0.4, $style, 'N');
// write rest of the letter
$this->WriteHTML($postnet_post[1], $this->line_height);
} else {
// no POSTNET so just write the whole letter
$this->WriteHTML($letter_html, $this->line_height);
}
When generating a barcode, make sure that you enclose the 12-digit delivery point inside of the "slash" character. Most POSTNET fonts render the slash character as the "control" character that pre/post-fixes the barcode values. Without those control characters the barcode isn't technically valid.
The POSTNET barcode font in TrueType format can be downloaded.
I tried the following and it worked:
$params = $pdf->serializeTCPDFtagParameters(
array('https://tcpdf.org/', 'QRCODE,H', '', '', 27, 27, '', 'N')
);
$html .= '<tcpdf method="write2DBarcode" params="'.$params.'" />';
Related
I am using mpdf to create PDF files from data extracted from a database. There are three sections in pdf which i need to display in TOC with their page number on which they are appearing. They can appear on separate page or on a single page as well when data is too short. Below is my code currently using:
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetFont('avenirnext');
$mpdf->SetTitle($title);
$mpdf->SetAuthor('Blavatnik');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($stylesheet2, 1);
$mpdf->WriteHTML($stylesheet3, 1);
$mpdf->setAutoTopMargin = 'stretch';
$mpdf->SetHtmlHeader($header, 'OE');
$mpdf->SetHTMLFooter($footer, 'OE');
$mpdf->AddPage('', // L - landscape, P - portrait
'', '', '', '', 20, // margin_left
20, // margin right
5, // margin top
25, // margin bottom
10, // margin header
10); // margin footer
$mpdf->WriteHTML($html);
$mpdf->Output($title . '.pdf', 'd');
I need to display TOC on first page and it also has some designing so I think it has to be done via html as displayed in example below:
https://mpdf.github.io/what-else-can-i-do/table-of-contents.html
I want to print HTML to PDF using TCPDF so I do this:
$html = '<h1>Hello</h1>
How are you?
<h2>Answer</h2>
I am well, thanks';
// ... etc. Long HTML document.
$pdf->writeHTML($html);
The question is if there is a way how to add a table of contents. If TCPDF can recognize page numbers of headings.
I also sometimes use HTML page breaks:
<div style="page-break-after: always"><span style="display:none"> </span></div>
I understand that I need bookmarks. But in this case TCPDF would have to create them automatically. If I add an ID to html-heading-tag, TCPDF probably does not find it and cannot use it.
<h1 id="abc123">header1</h1>
// but following does not do anything:
$pdf->addTOC()
Since the ToC page is created as the very last it will show that as the last page.
So instead use
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
//It will show page numbers
When using TCPDF together with FPDI templates, some CSS support is lost in the process. The problem are things like borders or background-colors, that end up in layers below the PDF template. TCPDF uses SetLineStyle() to convert CSS borders/backgrounds to PDF and this seems to be the problem.
For example:
$this->setSourceFile($filename); // /path/to/my/background.pdf
$imported_page = $this->ImportPage(1);
$this->useTemplate($imported_page);
...
$html = '<table style="border: 1px solid #000;"><tr><td style="background-color: #ff0000;">...</td></tr></table>';
$this->writeHTMLCell(45, 25, 160, 29, $html);
doesn't render the CSS borders. As soon as useTemplate() is removed the borders are there. Analyzing the resulting PDFs with Illustrator shows some interesting things:
PDF layers with useTemplate() - top to bottom:
Table/Content layers
PDF Template layers (group)
Border and background layers (paths)
PDF layers without useTemplate() - top to bottom:
Table/Content layers
Border and background layers (paths)
When disabling the layer group containing the PDF template in Illustrator, the borders and backgrounds become visible.
Unfortunately we didn't find a way to put the PDF template layer group at the bottom of the stack so everything else renders above it. The only workaround we could come up with, was wrapping the writeHTMLCell() call in startTemplate() / endTemplate() and finishing up with printTemplate():
$this->setSourceFile($filename); // /path/to/my/background.pdf
$imported_page = $this->ImportPage(1);
$this->useTemplate($imported_page);
...
$html = '<table style="border: 1px solid #000;"><tr><td style="background-color: #ff0000;">...</td></tr></table>';
$workaround_template = $this->startTemplate($w, $h);
$this->writeHTMLCell(45, 25, 160, 29, $html);
$this->endTemplate();
$this->printTemplate($workaround_template, $x, $y, $w, $h, 'T', 'L');
So out of curiosity: is this the only way to do it, or is there a way to put the PDF template at the bottom of all things to come?
Thanks in advance!
The solution is to use setPageMark() indeed.
Here is what worked very well for me:
public function AddPage($orientation = '', $format = '') {
global $pdf_data_path;
parent::AddPage($orientation, $format);
if ($this->use_headed) {
$this->setSourceFile($pdf_data_path."/headed.pdf");
$tplidx = $this->importPage(1, '/MediaBox');
$this->useTemplate($tplidx, 0, 0, 0, 0, true);
$this->setPageMark();
}
}
The key is to place setPageMark() after you used the template. The borders will then be stacked on top of the template in resulting PDF.
Did you try using the setPageMark()-method?
I am using this code to create an image
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
(A)print ('<div class="test">');
imagepng($im);
print ('</div>');
(B)imagedestroy($im);
?>
The code work fines if i comment the line number 'A' and 'B' and it generates the image on the browser with testing written on it. But i want the image to be in a div. so i uncomment the line (A) and (B) but it is not giving right output. The generated html is also strange generated html is
<img src="http://localhost/php/test92.php" alt="The image “http://localhost/php/test92.php” cannot be displayed, because it contains errors.">
Basically, to create dynamic image in HTML, you will need 2 PHP files:
one for the image itself
another one for PHP to display it.
Let's take a look how to do it:
You create image.php that accept parameter, like: image ID or file name. For security reason, you HAVE to filter whatever parameter it get.
Why you have to do this? because, to generate image, you can't mix it with another HTML output. Let alone a single space or return as this will render the image broken.
You do the HTML thing on another PHP, say test92.php. To the HTML logic here, like:
get image data
loop the data
display image => <img src="image.php?imageID=12" alt="" />
If you want a div around your image you have to do that in the html, you can't do that in the image generation code
<div>
<img src="http://localhost/php/test92.php">
</div>
If you are getting errors regarding the image, try browsing the image url http://localhost/php/test92.php and see what it looks like.
Does it show an image like you are expecting?
I'm trying to spice up my website by using custom fonts for headings. For me, the most appropriate way to do this is using PHP and GD. I have written a little script which will output the dynamic text based on the $_GET value, however sometimes the image is too wide, which moves everything else about.
How can I get the image to adjust the width of it, based on the width of the text? Here is the code I've written so far:
<?php
// Settings
$sText = $_GET['t']; // Text of heading
$sFont = "font/AvantGarde-Book.ttf"; // Default font for headings
$sMain = $_GET['c'] ? $_GET['c'] : 0xF2AB27; // Get a font or default it
// Create the image
header("content-type: image/png"); // Set the content-type
$hImage = imagecreatetruecolor(200, 24);
ImageFill($hImage, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($hImage, true);
imagealphablending($hImage, false);
imagettftext($hImage, 20, 0, 0, 24, $sMain, $sFont, $sText); // Draw the text
imagepng($hImage); // Generate the image
imagedestroy($hImage); // Destroy it from the cache ?>
Thanks!
Ok, I figured it out! For anyone else who may have this problem, you need to add:
// Calcuate the width of the image
$arSize = imagettfbbox(24, 0, $sFont, $sText);
$iWidth = abs($arSize[2] - $arSize[0]);
$iHeight = abs($arSize[7] - $arSize[1]);
Before the imagecreatetruecolor()
The function imagettfbbox will calculate the size of what the text will be based on the font you picked. Use the results in your call to imagecreatetruecolor.