PHP - Tcpdf And Images
Hi
I am trying to add an image to a pdf file created using tcpdf. Part of the code I have to date is: $pdf->AddPage(); // create some HTML content $htmlcontent = "<html> <strong>THIS IS AN EXAMPLE TITLE<br></strong> <p> This just some straight text that I've added to see what happens This just some straight text that I've added to see what happens This just some straight text that I've added to see what happens This just some straight text that I've added to see what happens This just some straight text that I've added to see what happens </p> <ol> <li>This is some example text for testing. <br> </li> <li>This is some example text for testing.</li> </ol> "; // output the HTML content $pdf->writeHTML($htmlcontent, true, 0, true, 0, ''); I have a logo which is in a folder and can be found at ../img/logo.png What I would like to do is have the image in the centre of the page before all of the text but whatever I've tried has failed. Any suggestions/answers would be much appreciated. Also I've failed at centering the title - any ideas on that. Many thanks. Similar TutorialsThis topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=332698.0 I wanted to do auto numbering for my tables with css using TCPDF. I tried both ways but it does not work at all. I tried the one in the documentation of tcpdf and it does not work too. Can anyone give me a piece of advice on how to make this work? These are the both ways i mentioned and it did not work. $htmlcontent .=' <style> table { counter-reset: rowNumber; } table tr::before { display: table-cell; counter-increment: rowNumber; content: counter(rowNumber) "."; padding-right: 0.3em; text-align: right; } </style> <table id="receiptTable" cellpadding="3" style="max-height: 1000px;"> <tr> <td width = "7%"style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> </td> <td colspan="3" style="font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> '.$irow['product_title'].' </td> <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> '.$irow['product_sku'].' </td> <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> '.$irow['quantity'].' '.$irow['quantity_unit'].' </td> <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> RM '.$irow['product_buy_price'].' </td> <td width= "20%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> RM '.$irow['purchase_price'].' </td> </tr> </table> '; }
for this i use the method of including external css into my tables but it does not work as well. I did make sure my file path is correct. (the code below) $htmlcontent .=' <style>.file_get_contents(css/receipt.css).</style> <table id="receiptTable" cellpadding="3" style="max-height: 1000px;"> <tr> <td width = "7%"style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> </td> <td colspan="3" style="font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> '.$irow['product_title'].' </td> <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> '.$irow['product_sku'].' </td> <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> '.$irow['quantity'].' '.$irow['quantity_unit'].' </td> <td style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> RM '.$irow['product_buy_price'].' </td> <td width= "20%" style=" font-weight: normal;font-size: 12px;line-height: 17px;color: #000000;"> RM '.$irow['purchase_price'].' </td> </tr> </table> '; }
Hi I am trying to work out how to add a loop statement to my pdf generation (I think that's what I need). I have a mysql table - events with 3 fields - id, name and date. In my php page which is to generate the pdf I have: mysql_select_db($database_events, $events); $result = mysql_query("SELECT id, date, name FROM events_pbw WHERE id>200 "); while($row = mysql_fetch_array($result)) { $id = $row['id']; $date = $row['date']; $name = $row['name']; } and in amongst the $html part of the page I have: $html ="<html> $id<br> $name<br> $date<br> <i>This is a test calendar.</i> This all works fine but (obviously) just gives one result. What do I need to add to make it list out all of the possible events? Many thanks in advance. I want to produce a pdf document using mysql and TCPDF. I can do this OK in some circumstances but I now have a problem. I have two tables - documents and parties - they are linked by a common field docid. I have for table documents, the fields: docid, doctitle, idcode and for the parties table: partyid, docid, party (the field party is the name of the party) For any document there could be any number of parties and for a single idcode there could be any number of documents. For example, for idcode = 12345 there might be 2 documents called doctitle 1 and doctitle 2. For doctitle 1 there are 3 parties to it party1, party 2 and party 3. For doctitle 2 there are two parties - party4 and party 5. For any idcode (which is passed through by the url) I want the pdf to give a list such as: Doctitle 1: party name 1 party name 2 party name 3 Doctitle 2: party name 4 party name 5 Part of my code is: mysql_select_db($database_process, $process); $result2 = mysql_query("SELECT parties.docid, GROUP_CONCAT(party SEPARATOR '<br>') AS partylist, documents.doctitle FROM parties INNER JOIN documents ON parties.docid=documents.docid WHERE parties.idcode = '$appidpassed' GROUP BY parties.docid LIMIT 1 ") or die(mysql_error()); while($row = mysql_fetch_array($result2)) { $doctitle = $row['doctitle']; $parties = $row['partylist']; } and then further down the page: $doctitle<br> $parties This only gives one document and the parties associated with it. I know the solution is probably easy but I can't see it. Does anyone have any ideas? Hi,
I'm using TCPDF to create a PDF from a webpage.
This works great but I have the following problem:
I'm building in WordPress and as you may know WP saves images on different sizes. (e.g. 150x150, 300x300 and 1024x1024)
The thing I'm trying to create is that users can download a PDF ready for printing. So High Resolution images.
I use a preg_match_all function to match the image source and remove the additional image size from the filename so the original high resolution image is loaded.
e.g. image-150x150.jpg will be replaced by image.jpg.
So now the original image that has a image size of 6024x6024px and 300 DPI is loaded.
Then TCPDF uses a resize function that resizes the image back to 150x150 px but sets the DPI to 300 so it can be printed on a good quality.
This all works but now the problem comes:
If I do the preg_match_all function the image gets "pulled" from the rest of the content and looses it's position.
So my thought to fix this was. Get a preg_replace function and replace the image tag with a function that executes the whole image change like described above.
This doesn't work......
So i'm stuck here. Hope anyone can assist me with this.
Here is what I have done so far.
function execute(){ preg_match_all("/<img[^>]+src=(?:\"|\')\K(.[^\">]+?)(?=\"|\')/",$content,$matches,PREG_PATTERN_ORDER); for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) { $search = array('-'.$s1.'x'.$s2.'', '-'.$s3.'x'.$s4.'', '-'.$s5.'x'.$s6.''); $replace = array('', '', ''); $a = $matches[1][$i]; if (strpos($a,'-'.$s1.'x'.$s2.'') !== false) { $w = $s1; $w = $pdf->pixelsToUnits($w); $h = $s2; $h = $pdf->pixelsToUnits($h); $l = '57'; $l = $pdf->pixelsToUnits($l); $t = '170'; $t = $pdf->pixelsToUnits($t); }elseif (strpos($a,'-'.$s3.'x'.$s4.'') !== false) { $w = $s3; $w = $pdf->pixelsToUnits($w); $h = $s4; $h = $pdf->pixelsToUnits($h); $l = '217'; $l = $pdf->pixelsToUnits($l); $t = '20'; $t = $pdf->pixelsToUnits($t); }elseif (strpos($a,'-'.$s5.'x'.$s6.'') !== false) { $w = $s5; $w = $pdf->pixelsToUnits($w); $h = $s6; $h = $pdf->pixelsToUnits($h); $l = '57'; $l = $pdf->pixelsToUnits($l); $t = '310'; $t = $pdf->pixelsToUnits($t); } $content .= $pdf->Image(str_replace($search,$replace,$matches[1][$i]), $l, $t, $w, $h, 'JPG', '', '', true, 300, '', false, false, 0, false, false, false); } }; global $post; $id = $_GET['id']; $content_post = get_post($id);// get the content $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = preg_replace('/<img[^>]+./e',execute(), $content); // replace the image tag with the new image // EXPORT IT ALL $pdf->writeHTML($content, true, false, true, false, ''); This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=323892.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=331758.0 Hi I have used TCPDF to generate documents with single values from a mysql table. Now I want to generate a calendar. I have a table events_pbw which contains (amongst others) the fields id, date and name. I would like to generate a pdf which lists all of the results for entries after a particular date (eg Jan 1st) I have this: // get data from events_pbw table $result = mysql_query("SELECT id, date, name FROM events_pbw WHERE `date` > 2011-01-01" "); while($row = mysql_fetch_array($result)) { $appid = $row['id']; $date = $row['date']; $name = $row['name']; } But I don't know how to get the full list of results into the pdf. I'm sure this is very easy but I'm not great on php coding! Does anyone have any examples or help they could give me? Many thanks Hi all, I am trying to get data from MySQL to display in a html table in TCPDF but it is only displaying the ID. $accom = '<h3>Accommodation:</h3> <table cellpadding="1" cellspacing="1" border="1" style="text-align:center;"> <tr> <th><strong>Organisation</strong></th> <th><strong>Contact</strong></th> <th><strong>Phone</strong></th> </tr> <tbody> <tr>'. $id = $_GET['id']; $location = $row['location']; $sql = "SELECT * FROM tours WHERE id = $id"; $result = $con->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { '<td>'.$location.'</td> <td>David</td> <td>0412345678</td> </tbody>'; } } '</tr> </table>'; Anyone got any ideas? I haven't worked with TCPDF yet (PHP class for creating PDFs) but if anyone has, I'm just curious if you can reference an external CSS file to format the HTML that gets created in the PDF? Or can you only use inline CSS? Basically, I have a nicely formatted HTML table using a somewhat complicated external CSS file and I want to output that same table to a PDF and have it look essentially the same. If TCPDF cannot do this, do you know of any other PDF PHP libraries that can? Thanks, Greg Hi I have a table (applicant) and I am looking at producing a pdf displaying various items of data from this table. I have succeeded in getting the look I want but in my query I use a fixed applicantid (in this case an id of 4). What I want to be able to do is to have the id cary according to the url. In other words, when someone comes from one web page to this one and the url is of the format .../pdf_production.php?applicantid=42. I'm not sure I've explained that very well but I hope someone can help - I think this is the last thing I need to sort out. Many thanks in advance. The code I have so far is: <?php require_once('../Connections/process.php'); ?> <?php require_once('../tcpdf/config/lang/eng.php'); require_once('../tcpdf/tcpdf.php'); // get data from users table mysql_select_db($database_process, $process); $result = mysql_query("SELECT * FROM applicant WHERE applicantid = '4'"); while($row = mysql_fetch_array($result)) { $appid = $row['applicantid']; $idcode = $row['idcode']; $type = $row['type']; $company = $row['company']; $email = $row['email']; } // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $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 some language-dependent strings $pdf->setLanguageArray($l); // --------------------------------------------------------- // set font $pdf->SetFont('dejavusans', '', 10); // add a page $pdf->AddPage(); // create some HTML content $txt = <<<EOD Below are the details I require Company type: $type Company Name: $company Company email: $email EOD; // output the HTML content // $pdf->writeHTML($htmlcontent, true, 0, true, 0); $pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0); // $pdf->writeHTML($inlinecss, true, 0, true, 0); // reset pointer to the last page // $pdf->lastPage(); //Close and output PDF document $pdf->Output('example_006.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>
Hi, I have a table of content in my page. For each row it has a check box. How do i achieve the function of when i click on submit button, those rows which are checked will be downloaded as a single pdf file. For example, for each checked row is a PDF file here. Suppose if i checked for 5 rows , 5 PDFs will be downloaded. Hi all!
I'm new here, apologies that my 1st post will be a cry for help. I'm tearing my hair out with this one.
I've got this page that generates PDF's via TCPDF plugin http://www.kinho.com/lightbox/ whenever I view it in chrome it zooms in at 100% to the top left corner. The problem is I need the images to be large and suitable to print (7"x5" or thereabouts) and so the PDF isn't built to be viewed at 100%, it just looks too zoomed in. Every other browser respects the fit to width setting. My images are 1600px on their long side, pages are landscape. here's the create.php:https://dl.dropboxusercontent.com/u/14123055/PHP/create.php
Can anybody advise a workaround for chrome? many thanks!
badger
I have this script from http://lampload.com/...,view.download/ (I am not using a database) I can upload images fine, I can view files, but I want to delete them. When I press the delete button, nothing happens
http://www.jayg.co.u...oad_gallery.php
<form>
<?php $dir = dirname(__FILENAME__)."/images/gallery" ; $files1 = scandir($dir); foreach($files1 as $file){ if(strlen($file) >=3){ $foil = strstr($file, 'jpg'); // As of PHP 5.3.0 $foil = $file; $pos = strpos($file, 'css'); if ($foil==true){ echo '<input type="checkbox" name="filenames[]" value="'.$foil.'" />'; echo "<img width='130' height='38' src='images/gallery/$file' /><br/>"; // for live host //echo "<img width='130' height='38' src='/ABOOK/SORTING/gallery-dynamic/images/gallery/ $file' /><br/>"; } } }?> <input type="submit" name="mysubmit2" value="Delete"> </form>
any ideas please?
thanks
Ok, I have a GD image that is then rewrote as a png. My question is, is it possible to find out what site is requesting the image and then use it on the php image. Example, if I didn't want a domain to use my images, could I somehow me able to restirict that site? Hey every i'm just trying to change my 'Read More' text after a blog post on my site into am image. Got some code that lets me change the actual text to something else, just can't find how to change the text into an image. Code: [Select] add_filter( 'the_content_more_link', 'child_content_more_link', 10, 2 ); function child_content_more_link( $more_link, $more_link_text ) { return str_replace( $more_link_text, 'my new read more text', $more_link ); } Thanks, Matty44m Hi guys I just want to know something. With php you can handle excel files by saving them as .csv files. Then the php script reads it line by line, and commas separating the fields. But is it possible that I can insert an image (small jpg or png) file in one of these fields...and then handle the image in the php script? Just for intrest I inserted an image in excel worksheet and it did not insert it into a cell, it float. Is there maybe another to insert it in the cell and then control it with php?? Thanx in advance I have a site where I need to have lets call it image1 displayed, then I want to change this image based on a php if statement, for instance: if $var == $var2 change the image ....blah blah so I was also going to have the names of my images stored in my database, i.e. image1.jpg and image2.jpg in my database. the image is in its own div tag set as the background image of the div tag if that makes any diference. Thanks Hi guys, i have a form that uploads a school photo. All i want to do is resize the image to its widest dimension of a width of 480 px and then obviously constrain the height. can anyone help me, ive tried to do this myself but when it comes to image properties i really struggle etc. heres my upload code. Code: [Select] <?php include("includes/connection.php"); // Where the file is going to be placed $schoolimage = "SchoolImages/"; //This path will be stored in the database as it does not contain the filename $currentdir = getcwd(); $path = $currentdir . '/' . $schoolimage; // Get the schoolid for the image and school linker table $schoolid = $_POST['schoolid']; //Get the school name $query = "SELECT * FROM school WHERE school_id = ".$schoolid; $result = mysql_query($query) or die("Error getting school details"); $row = mysql_fetch_assoc($result); $schoolname = $row['name']; //Use this path to store the path of the file in the database. $filepath = $schoolimage . $schoolname; //Create the folder if it does not already exist if(!file_exists('SchoolImages')) { if(mkdir('SchoolImages')) { echo 'Folder ' . 'SchoolImages' . ' created.'; } else { echo 'Error creating folder ' . 'SchoolImages'; } } //Store the folder for the course title. if(!file_exists( $filepath )) { if(mkdir( $filepath )) { echo 'Folder ' . $schoolname . ' created.'; } else { echo 'Error creating folder ' . $schoolname; } } // Where the file is going to be placed $target_path = $filepath; // Add the original filename to our target path. Result is "uploads/filename.extension" echo $target_path = $target_path . '/' . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded"; $filename = $_FILES['uploadedfile']['name']; //Store the filename, path other criteria in the database echo $query = "INSERT INTO image(image_id, name, path) VALUES(0, '$filename', '$filepath')"; //Perform the query $add = mysql_query($query, $conn) or die("Unable to add the image details to the database"); $imageid = mysql_insert_id(); //Store the filename, path other criteria in the database echo $query = "INSERT INTO image_school( image_id, school_id ) VALUES('$imageid', '$schoolid')"; //Perform the query $add = mysql_query($query, $conn) or die("Unable to add the image details to the database"); $message = 'Upload Successful'; } else { $message = 'There was an error uploading the file, please try again!'; } //Close the connection to the database mysql_close($conn); header("Location: add_school_photo_form.php? message={$message}&schoolid={$schoolid}"); //header("Location: add_school_photo_form.php? message=$message, schoolid=$schoolid"); exit(); ?> Id be eternially gratefull. Kind Regards Dean |