Related
I'm trying to use FPDF and FPDI to edit a PDF and add text to it. I keep getting an "Incorrect output destination" error but the destination is the correct location that I want it to create a file in, why does FPDF not like my output destination?
This is in a laravel project
$pdf = new \setasign\Fpdi\Fpdi();
$pdf->AddPage();
$pdf->setSourceFile(public_path('/pdf/higher.pdf'));
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 100);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output(public_path('/pdf/'),'higher2');
return $pdf;
and the error is:
message: "FPDF error: Incorrect output destination: /home/vagrant/code/project-name/public/pdf/"
I've also tried removing the "public_path()" and just setting it to Output('pdf', 'higher2') and no good there either.
Furthermore I've also tried changing the name of the output pdf to higher2.pdf just in case it wanted to see the extension (but obviously it's having more of a problem with the destination and not the name)
I've even tried changing permissions on this folder to be writable by anyone:
drwxrwxrwx 5 ion staff 160 May 21 05:44 pdf
edit: Just to note I see that the method with the public_path() is trying to save to my vagrant folder for some reason, that's part of the reason I'm confused. When I try to save to '/pdf' without public_path(), I get this error:
message: "FPDF error: Incorrect output destination: /pdf/"
edit 2:
I've also tried this:
$pdf->Output('F','/pdf/higher2.pdf');
and got the error:
message: "file_put_contents(/pdf/higher2.pdf): failed to open stream: No such file or directory"
and also tried the original name of the pdf which definitely exists and got the same error:
$pdf->Output('F','/pdf/higher.pdf');
You should never overwrite the file you are reading from!
The signature of the Output() method is:
string Output([string dest [, string name [, boolean isUTF8]]])
The $dest parameter is defined as:
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
So your code:
$pdf->Output(public_path('/pdf/'),'higher2');
makes absolutely no sense. I guess you want to save the resulting PDF to the path in the public area with the name higher2.pdf. So your code should look like:
$pdf->Output('F', public_path('/pdf/higher2.pdf'));
PS: You cannot edit a PDF with FPDI!
The Output() method requires the first parameter to be the destination and the 2nd parameter the filename.
From the documentation:
F: save to a local file with the name given by name (may include a path).
Try this:
$filename="/pdf/higher2.pdf";
$pdf->Output($filename,'F');
For the FPDF package, the syntax $pdf->Output('F','/pdf/higher2.pdf'); is wrong and you need to adjust your call as Jan Slabon explained.
However, if you want to support UTF-8 characters, then you need the tFPDF package, which is also supported by the setasign vendor:
$pdf = new \setasign\Fpdi\Tfpdf\Fpdi();
For this package you can store the output like this:
$pdf->Output('/pdf/higher2.pdf');
I'm using the TCPDF library to generate a PDF file with PHP. I want to use a custom font so I used the addTTFfont method to add my custom TrueType font files. The font I am trying to add is "Aller" from fontsquirrel.com
$aller = $pdf->addTTFfont($_SERVER['DOCUMENT_ROOT'].'/includes/fonts/Aller/Aller_Rg.ttf', 'TrueTypeUnicode', '', 32);
This created the files aller_rg.php, aller_rg.ctg.z, and aller_rg.z in my TCPDF fonts folder. The K_PATH_FONTS constant points to this directory. The addTTFfont method returns the string name of the font. It would return false if there was an error so the problem is probably not here..
I then tried to use the font
$pdf->SetFont($aller, '', 16); // or $pdf->SetFont('aller_rg', '', 16);
$pdf->Write(0,"abcdefg",'',0,'L',true,0,false,true,0);
The pdf is generated without any errors. When viewsed in the browser preview the font is clearly not aller but just a generic sans-serif.. When I open the pdf in Mac's Preview application the sections that are using the aller font are blank (no text displays).
Anyone know what I'm doing wrong?
Got it to work by using this tool
http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf
it generates a .php, .z, and .afm file that you put in the TCPDF fonts directory. I'm still not sure what the problem was. the addTTFfont() method doesn't create the .afm file so maybe that's it?
I had the same problem:
The TCPDF converted font did not display, but the one converted with the following link did:
http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf
So I compared the two and found out that xml-convert.com was using "TrueType" instead of "TrueTypeUnicode" as font type. For me it worked to change the argument to "TrueTypeUnicode". Try:
$aller = $pdf->addTTFfont($_SERVER['DOCUMENT_ROOT'].'/includes/fonts/Aller/Aller_Rg.ttf', 'TrueType', '', 32);
This is the Link I am following to create an image, however my GD is installed and working correctly as i tested with the very first example but this code breaks and flag "Image can not be displayed because it contains error" .
CODE -
<?php
//phpinfo();
//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL);
//Set the content type
header('content-type: image/png');
//Create our basic image stream 300x300 pixels
$image = imagecreate(300, 300);
//$image = imagecreatetruecolor(300, 300);
//Set up some colors, use a dark gray as the background color
$dark_grey = imagecolorallocate($image, 102, 102, 102);
$white = imagecolorallocate($image, 255, 255, 255);
//Set the path to our true type font
//$font_path = 'advent_light';
//$font_path = 'advent_light.ttf';
//$font_path = 'arial';
$font_path = 'arial.ttf';
//Set our text string
$string = 'Swapnesh!';
//Write our text to the existing image.
imagettftext($image, 50, 0, 10, 160, $white, $font_path, $string);
//Create our final image
imagepng($image);
//Clear up memory
imagedestroy($image);
?>
Things I tried and googled but with no solution are as follows - :(
Checked white/blank spaces before php tag if any.
Remove all code and spaces before header() method.
Changed imagecreate() to imagecreatetruecolor() as in comments in the code.
Checked font_path and set font path as per comments as well.
Followed this & this.
My PHP version - PHP Version 5.3.8
Still unable to locate the problem :(
Error
EDITS-
version 1.0
this is what im getting on saving.
More Info about image --
on saving the page -- this is the name of file saving -- create_png.php.png
version 1.1
<br />
<b>Warning</b>: imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\Core\CoreFiles\create_png.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>: imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\Core\CoreFiles\create_png.php</b> on line <b>23</b><br />
Thanks to #cryptic and #user1711126
Solution --
Font file actually missing, we need to put our .ttf file under the folder to make this code working or set path to make it work.
Save the image file that errors and open it in a text editor like notepad or equivalent and see if any PHP errors are inside it. An error is occurring and outputting text then which corrupts the file. Do that and you will find what the error is.
I think #cryptic is right you need to use the real path for font use something like
$font_path = basename(dirname(__FILE__)).'/arial.ttf';
and please make sure the font exists in the directory.
I seriously doubted your font path, please double check your font and file in same directory and if it is, then change path like this,
$font_path = './arial.ttf';
Edit (As per Discussion in chat)
you must need an ttf file to use imagettftext() function place the arial.ttf file in your folder where your file reside
I am trying to generate a PDF with an image that is also generated by php.
Sounds simple enough and I am sure I'm just screwing up the header but I can't seem to find a solution here.
first I generate a PDF:
define('FPDF_FONTPATH','fonts/');
require('scpt/fpdf.php');
class PDF extends FPDF {}
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->SetAutoPageBreak(false,0);
$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(0,0,0);
$pdf->SetFont('Helvetica','',12);
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
then I generate the PNG in label.php:
if(isset($_GET["imgid"])) {
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
}
This will output: FPDF error: Not a PNG file:....
calling label.php?imgid=17 in the browser however will show me the image just fine...
What am I missing?
EDIT
In document:
Example
// Insert a logo in the top-left corner at 300 dpi
$pdf->Image('logo.png',10,10,-300);
// Insert a dynamic image from a URL
$pdf->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World',60,30,90,0,'PNG');
so it SHOULD be possible to include a dynamically generated image without saving it first?!
EDIT #2
I made it work with the library mem_image but the problem remains that this should not throw an error IMO?! So I leave this question open to see if there is indeed something wrong with my script of this turning out to be a bug.
You almost had it, you're missing only a couple things. First you need the full server address in the URL for your Image() call, so instead of
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
You need:
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
That will eliminate the FPDF error you were encountering. Then to get FPDF to render your output correctly, you need to add a call to AddPage(), so your PDF generation script would become:
require('scpt/fpdf.php');
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->AddPage();
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
Of course you don't need the SetTextColor(), SetFont(), etc if you're only including the single image. You didn't need the custom class definition either (I removed the unneeded lines).
Don't forget to substitute www.yourserver.com for the appropriate domain and path for your script.
The argument to the Image function needs to be a file, not a URL or another script. Since you already have your label script, the easiest work around would be to download the image to a temp location and then call $pdf->Image() with that temporary file.
This problem occurred With your file name,Make sure your passing file name is valid
function _parsepngstream($f, $file)
{
// Check signature
if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
$this->Error('Not a PNG file: '.$file);
...
}
View allow_url_fopen directive
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
You need to do only changes that
require('fpdf.php');
and do the
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
and run your code...
First, try to check your url with this code
$handle = fopen("http://www.yourserver.com/label.php?imgid=17", "rb");
print_r(fread($handle, 8192));
you with see what the contain in your url
**) make sure your url is not redirect to the login page
much more easy, only put .png in the final of dinamic chain and its all
example:
$pdf->Image('http://www.yourserver.com/label.php? imgid=17.png',0,0,0,0,'PNG');
or
$pdf->Image('../label.php?imgid=17.png',0,0,0,0,'PNG');
the mime type is not recognized in the url why does not appear png, if it's a stupid mistake but it is proven, google does the same by adding the extension with the url & PNG
I want to make a unique forum signature from my latest watched animes in PHP. These are contained in an RSS feed. On my local apache server, the image is generated well, but as I upload it onto a server, I get an error, the picture isn't generated at all.
Here is my code and I wonder what's the problem since neither Dreamweaver CS5 or phpDesigner 7 don't show any errors, although if I press the Run button in phpDesigner, I get an error, but I don't know what it means. The error is the following:
Line: 6 - Fatal error: Call to undefined function imagecreatefrompng() in [php's path here]
So the code is the following:
<?php
// title & description arrays
$titleCuts = array();
$descCuts = array();
// bg image
$bgimg = imagecreatefrompng('sig_robin.png');
// colors
$white = imagecolorallocate($bgimg, 255, 255, 255);
$textColor = imagecolorallocate($bgimg, 245, 193, 9);
$shapeColor = imagecolorallocate($bgimg, 27, 20, 0);
// sxe <- xml url
$sxe = new SimpleXMLElement('http://myanimelist.net/rss.php?type=rw&u=fema', NULL, TRUE);
// shape
imagefilledrectangle($bgimg, 255, 20, 567, 279, $shapeColor);
// TEXTS
imagettftext($bgimg, 20, 0, 263, 52, $white, "my.ttf", "Latest Watched:");
// episodes' text
for($i=0;$i<5;$i++)
{
// title cut and joint
$titleCuts = explode(' ', $sxe->channel->item[$i]->title, -2);
$titleCut = implode(' ',$titleCuts);
// description (ep) cut and joint
$descCuts = explode(' ', $sxe->channel->item[$i]->description);
// output
imagettftext($bgimg, 10, 0, 270, 77+($i*45), $textColor, "my.ttf", $titleCut);
imagettftext($bgimg, 10, 0, 270+(strlen($titleCut)*7.2), 92+($i*45), $textColor, "my.ttf", "ep. ".$descCuts[2]);
}
header('Content-type: image/png');
// generating image
imagepng($bgimg);
?>
Thanks in advance.
Edit:
As I removed the header, now I get a lot of error that it can't find the font file, but I'm dead sure that I wrote it correctly.
They look like this:
Warning: imagettftext() [function.imagettftext]: Could not find/open font in ... on line 19
This means that GD either wasn't compiled into PHP or hasn't been loaded. First, check your php.ini, the path for which you can find with phpinfo() for extension=php_gd2.dll and make sure it's not commented out with a semicolon. After changing the setting, restart the webserver, then look at phpinfo() again to see if GD is loaded.
IF imagecreatefrompng is not defined, most likely the GD library is not installed, or broken, see: http://php.net/manual/en/image.installation.php
Don't be alarmed by the 'you have to configure and rebuild PHP', normally the package manager of your OS can easily add GD support by installing an extra package.
Ok, the solution for the second problem is that its a php bug http://bugs.php.net/bug.php?id=41336
But its easy to solve, in following way:
Put a ./ before every *.ttf file like in following example:
imagettftext(IMAGE, 0, 0, 0, 0, TEXT_COLOR, "./ttf_file.ttf", TEXT);
To use this function (imagecreatefrompng) your php needs to be installed with the GD extensions (for more information see this page.
To check what is / isn't install create a php file on your local server with just:
And then compare the image (GD) sections to the same file running on your server.
Note that if you are on a shared hosting server you'll probably have to contact their admins to install / configure this for you.