I want to emulate generating a "sticky note" in PDFs. My current system is a wordpress website with a custom plugin written in PHP that uses fpdf for PDF generation. I can generate a yellow rectangle with borders and text easily:
$pdf = new fpdf();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->SetFillColor(255, 246, 108);
$pdf->SetXY(0, 40);
$pdf->ShadowCell($pdf->GetPageWidth(),50,'Hello World!', 1, 0, 'C', true);
$pdf->Output();
Now the issue is generating a shadow around the box. I've gone through the fpdf API and examples, but didn't see anything relevant that I could use. There is a text-shadow example, but that doesn't work in my case.
Related
everyone I am working with fpdf an fpdi, is my first work and I have a problem. I can not edit a pdf. This is the problem. thanks
Class setasign\Fpdi\FpdfTpl not found in C:\wamp\www\Ale\fpdi\src\Fpdi.php
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi/src/Fpdi.php');
require_once('fpdi/src/autoload.php');
// initiate FPDI
$pdf = new Fpdi();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile('documento.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at position 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);
// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output('newDoc.pdf','F');
You cannot edit a PDF with FPDI!
Remove the line:
require_once('fpdi/src/Fpdi.php');
...to give the later required autoload function a chance.
Maybe not related to this exact question but I had a similar situation in my Lumen project...
I resolved it by reintsalling vendor files. I hope if someone is facing similar situation, I hope this helps.
I'm trying to add images on top of an existing PDF.
The pdf contains a blank grid, I am writing a script that will insert images on top of the PDF and output a new modified PDF file.
I am using FPDI and TCPDF libraries to load and edit the PDF file.
Any image or text i try to add using Write(); and Image(); functions does not appear anywhere in the Output file.
<?php
// defining encoding and linking libraries
header('Content-Type: text/html; charset=utf-8');
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
// Creating new page with PDF as a background
$pdf = new FPDI();
$pdf->setSourceFile("resources/worksheet_template.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
// $pdf->MultiCell(0, 5,'$pdf', 0, 'J', 0, 2, '', '', true, 0, false);
$pdf->Image('resources/lantern.png', 50, 50, 100, '', '', 'http://www.tcpdf.org', '', false, 300);
ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');
?>
This script works without problems! Expecting that all resources are accessible. The MultiCell call looks something special but as you'd used single quotes at least the string $pdf will appear in the resulting document. Furthermore the header() is obsolete too. Make sure that your PHP file is saved without a BOM to get rid of the ob_clean() call, too.
Generally it should be a good practice to define a font and size before writing any text but it seems that TCPDF handles this internally by a standard font for you.
Also make sure that you are using the latest version of both FPDI and TCPDF.
I have a Facebook app with which I'd like to display an image in a user's feed. It's just a small circle, which will be different colours depending on what the user does within my app.
Instead of creating lots of different images to display, one for each possible colour the circle could be, it would be ideal if I could put up a PNG with transparency, and then just change the colour by filling a DIV behind the image with Javascript.
However, it's not clear to me if Facebook will allow that in the feed.
Can include some Javascript in the feed, or is that strictly forbidden?
Or is my only option to have a library of images for all the different colours and have a PHP function which selects the right one to output?
Facebook doesn't allow any scriptable content within feed stories, and this isn't possible to implement what you want this way.
You can easily create simple script that will return colored image according to passed arguments and use it as source for the image.
Something like this may provide you some points:
<?
// create a 200*200 image
$img = imagecreatetruecolor(200, 200);
// get the color from URL arguments or use default one
$rgb = isset($_REQUEST['color']) ? $_REQUEST['color'] : 'FFEEDD';
$color = array(
base_convert(substr($rgb, 0, 2), 16, 10),
base_convert(substr($rgb, 2, 2), 16, 10),
base_convert(substr($rgb, 4, 2), 16, 10),
);
// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, $color[0], $color[1], $color[2]);
// draw the head
imagefilledarc($img, 100, 100, 200, 200, 0, 360, $red, IMG_ARC_PIE);
// output image in the browser
header("Content-type: image/png");
imagepng($img);
// free memory
imagedestroy($img);
No, facebook doesn't allow embedding JavaScript in feeds for security reasons. Even if you manage to do it, it won't work because certain characters such as <, >, etc will be converted into html entities which means the JavaScript won't work.
I have a website I am building that takes data (the person's name) from a form and posts it into a customized "note" in a table on the next page. I want the user to then be able to save their "note" as an image with their customized name on it. Is there a way to save a SPECIFIC HTML TABLE as a PNG? Maybe take a screenshot of a specific coordinate on a web page?
If not is there a way to render posted PHP content from the form in an HTML canvas?
Any help would be great. I'm in a little over my head right now.
While you can render HTML using a variety of tools, you can't be sure that those tools will render the HTML the same way your browser renders it.
If your end goal is to generate an image with text on it, then let's solve that problem instead of trying to make the solution you suggested work.
Have a look at PHP's imagettftext() function. It contains an Example #1 which creates a small, simple image from text that can be stored in any variable ... including a form variable.
By using this example, and adding a few other of PHP's GD functions, you can make a decent replica of a table, and make sure it looks exactly the way you want it, rather than the way html2ps or some other tool renders it.
<?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 = isset($_POST['name']) ? $_POST['name'] : "name";
// 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()
imagepng($im);
imagedestroy($im);
?>
Here's sample output generated by the above script:
Note that you'll need to provide arial.ttf per the instructions at the link above.
If things don't work, look for errors both on-screen and in your web server's error log FIRST, before following up here. It's possible that PHP's GD module is not installed on your web server. If this is the case, you should check with your server administrator to make sure what you need is available to you.
You can try this JS library on client side.
workable solution
use fpdf to create pdf from html (table)
use imagemagick to convert pdf to png
You can also use Xvfb. It's a linux package. It stands for "X-window Virtual Frame Buffer" (if you were wondering why on earth it had such an esoteric name).
What that will do, is load a page, execute any JavaScript and apply the different stylings from CSS, all in the frame-buffer of the server. Then you can save the image.
Xvfb will probably not be available on most shared-hosting services, however, if that doesn't matter, then it would be a viable solution.
You can use something like html2ps
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.