FPDF Error - Can't get image to appaear on PDF - image

I have a PDF with fillable forms. I can successfully fill the forms and save the new PDF to my server no problem, but I cannot place an image on the PDF. When I remove the Image(....) line the script works great. When I add it back it I get the error.
I think it's trying to look for the method in the wrong file, originally I only included fpdm..php but I tried adding back fpdf.php and it did not help. Not sure what I'm doing wrong.
Error:
Fatal error: Call to undefined method FPDM::Image() in /home/.../formPDF.php on line 113
Code:
require('../forms/pdf/fpdf.php');
require('../forms/pdf/fpdm.php');
$pdf = new FPDM($formPDFLocation);
$pdf->Image('images/sig_37-1405313221.png', 100, 20);
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
$filename= "../forms/generated/" . $ffID;
$pdf->Output($filename.'.pdf','I');

FPDM is not really related to FPDF but simply a script of a FPDF user. You cannot use FPDF methods on an FPDM instance.
If you search for an all in one solution for such task you may take a look at the SetaPDF-FormFiller component (not free!).

I was able to save the PDF to my server and then add the image but lost the form contents. Code for anyone that is interested in this half solution:
Code for adding image:
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile("16.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 100);
$pdf->Image('car.jpg',20,100);
$pdf->Output();
?>

Related

Streaming file to laravel storage

I am using JPGraph to generate a graph, the hope is to then save the graph to the file system and point to it in a laravel view. I am attempting to render that view into a PDF later (this is why I'm not generating the chart on the fly).
The problem I am having is I can't figure out how to get the Stream method in JPGraph to save to the Laravel file system.
$graph = new Graph\PieGraph(350, 250);
$graph->title->Set("A Simple Pie Plot");
$graph->SetBox(true);
$legends = ['A (%d)','B (%d)','C (%d)','D (%d)','E (%d)',];
$data = array(40, 21, 17, 14, 23);
$p1 = new Plot\PiePlot($data);
$p1->SetLegends($legends);
$p1->ShowBorder();
$p1->SetColor('black');
$p1->SetSliceColors(array('#1E90FF', '#2E8B57', '#ADFF2F', '#DC143C', '#BA55D3'));
$graph->legend->SetPos(0.5,0.98,'center','bottom');
$graph->Add($p1);
$image = $graph->img->Stream(/* TO LARAVEL STORAGE */);
I have tried pointing directly to the path using storage_path('images\graphs\test.png'); but I get a permissions error.

Telegram's instant view: fails to load .webp and .ico images

I have a problem when trying to create my Telegram's Instant View template, with this error:
Resource fetch failed: https://gdude.de/blog/assets/images/Kaggle-Lyft/task.webp
Resource fetch failed: https://gdude.de/blog/assets/images/telegram.ico
The URLs are valid, I have checked.
These are the only two images that fail. Does IV support *.webp and *.ico images?
According to their manual, Instant View is only actually supporting gif, jpg and png.
with the attribute src and the optional attribute href to make the image clickable. Allowed formats: GIF, JPG, PNG (GIF would be converted into Video type by IV)
I had a similar problem and solved it in the following way.
Note: You need a hosting server to store a PHP script, a free one worked for me (000Webhost).
The diagram below represents the general idea
Instant View code
Note: I'm a beginner at Instant View and XPath, so for now I'm just writing code that works.
# Find unsupported images
$imgs: //img[ends-with(#src, ".webp")]
$imgs+: //img[ends-with(#src, ".ico")]
# Temporary element to create the URLs and make calls to the conversion service
#html_to_dom: "<a>"
$tmp_tag
# For each unsupported image
#map($imgs){
$img: $#
# Build de URL to request the image conversion service
#set_attr(href, "https://my-free-subdom.000webhostapp.com/imgconverter.php?url=", $img/#src): $tmp_tag/a
# Make the request
#load: $tmp_tag/a/#href
# Change the src of the unsupported image to that of the converted image created by the conversion service
#set_attr(src, $#//img/#src): $img
}
#remove: $tmp_tag
PHP script to convert the image
To handle the ICO files I used the IcoFileLoader library, I found it thanks to this question PHP GD .ico handling. I just took the PHP files from the src directory and put them directly on my hosting, so I had to use a simple Autoloader.
// The URL of the image to convert
// If the url of the image is relative, you have
// to build it here, example $url = 'https://gdude.de'.$_GET['url'];
$url = $_GET['url'];
// File name
$file_name = basename($url);
// Directory where the image will be saved
$dir = './';
// File location
$save_file_loc = $dir . $file_name;
// Open file
$fp = fopen($save_file_loc, 'wb');
// Download the image using CURL
$ch = curl_init($url);
// Set options for a cURL transfer
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
// Close file
fclose($fp);
// Load the image
// ICO images need special handling
if(str_ends_with($file_name, '.ico'))
{
require_once('Autoloader.php');
$loader = new IcoFileService;
// You must define the size, I did the tests with a 16X16 favicon.
$im = $loader->extractIcon($file_name, 16, 16);
}
else if(str_ends_with($file_name, '.webp'))
{
$im = imagecreatefromwebp($file_name);
}
// Check if the image was loaded
if(!isset($im))
{
die('Unable to load image!');
}
// Convert it to a png file
imagepng($im, $file_name.'.png');
imagedestroy($im);
// Delte the original image
unlink($file_name);
// "Return" the image in an HTML tag so that Instant View can handle it
echo '<img src="https://my-free-subdom.000webhostapp.com/' . $file_name . '.png">';
Feel free to improve the above code, perhaps add some security, delete old images, use other libraries or PHP functions, accept multiple images in the same request, etc.
I found imageoptim to be helpful for free conversion of (in my case) svg images. Just prepend this url to the svg url and they'll start to load. I chose a resolution of 2560 as it's the max resolution that IV 2.1 supports.
#set_attr(src, "https://img.gs/<your-username>/2560,fit,format=png,quality=high/", ./#src): $body//img[contains(#src, ".svg")]
#set_attr(srcset, "https://img.gs/<your-username>/2560,fit,format=png,quality=high/", ./#srcset): $body//img[contains(#srcset, ".svg")]

tcpdf create a label pdf size width:57mm and height:32mm

guys i use TCPDF library for my php server and i want to create a label with width:57mm and height:32mm
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage('L', array(57,32));
$pdf->SetFont('dejavusans');
$html ="
<table class='domi'>
<tbody >";
for($i=0; $i<$rows; $i++){
$style=' style="font-size:'.$fontsize[0][$i].'px"';
if($bold[0][$i]===0){
$html2a[$i] = "<tr><td".$style.">".$content_line[$i]."</td></tr>";
} else {
$html2a[$i] = "<tr><td".$style."><b>".$content_line[$i]."</b></td></tr>";
}
$html2b .= $html2a[$i];//$pdf->GetStringWidth($html2a[$i])." ";
}
$html3 = "</tbody>
</table>";
$pdf->writeHTMLCell($w=55, $h=2, $x='1', $y='1', $html.$html2b.$html3, $border=1, $ln=1, $fill=0, $reseth=false, $align='C', $autopadding=true);
$pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'Site/labels/label'.$id.'.pdf', 'FI');
1st of all it prints the contents fine from the database in A4 size etc but all i want is to create and save the pdf document in the label size width:57mm and height:32mm!!! https://i.stack.imgur.com/kQ8Be.png
as you see, I've marked with 1-5 numbers the actual data and the rest are marked with questionmarks!! I dont understand why of course.
2nd when i change the line $pdf->AddPage('L', array(57,32)); to $pdf->AddPage('L', array(57,57)); the label is created but i have an empty space in the bottom-right corner + it doesnt have the 32x57 size!!! https://i.stack.imgur.com/XgEWU.png
thanks in advance!!!
it seems that i had missconfigured my TCPDF library...
the simple solution that i came up was to put this line
$pdf->SetAutoPageBreak(true, 0);
after this
$pdf->SetFont('dejavusans');
and the result was perfect!!!
https://i.stack.imgur.com/hGwyR.png <-perfectly configured label
https://i.stack.imgur.com/khs9v.png <-wrongly configured label
but in my case it perfect for the job!!! all i want is to preview the result before saving (i mean saving the wrong configuration and then update the base until the configuration is good!!)

Magento - Category Thumbnails - How to use SVG?

I cannot upload SVG-images as Category-Thumbnail-pictures in Magento (CE, 1.8). Just "normal" image-formats like jpg or png seem to work.
Does anybody have an idea?
You need to rewrite or modify this Model
class Mage_Catalog_Model_Category_Attribute_Backend_Image
app/code/core/Mage/Catalog/Model/Category/Attribute/Backend/Image.php
In line 60
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png','svg')); // add the file type SVG to the allowed extension array.
It will work just fine
Soltion is:
class Mage_Catalog_Model_Category_Attribute_Backend_Image
rewrite file
app/code/core/Mage/Catalog/Model/Category/Attribute/Backend/Image.php
update code
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png','svg'));
$uploader->setAllowRenameFiles(true);
// Avoid Validation for SVG
// $uploader->addValidateCallback(
// Mage_Core_Model_File_Validator_Image::NAME,
// new Mage_Core_Model_File_Validator_Image(),
// "validate"
// );
And boom , you are done!!

How to generate pdf files in Magento

I have to generate a prduct list within a pdf file in Magento.
How can I use Zend_Pdf to do that ?
Thanks a lot.
I've found Zend_Pdf to be a little challenging to use. The way I was using it was to create a "template" pdf in some other program (i used open office calc). Saving that somewhere in the magento installation and calling
$pdf = Zend_Pdf::load('filename.pdf');
The next is to modify the pages. Mine only had one page, so I used
$page = $pdf->pages[0];
This grabs the first (and only page). To use text you have to set the font. Then draw text.
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 8);
$page->drawText($currentDate, 490, 702);
You said you would like to create a list of products, so you could do something like. It's very hackish, since you have to draw text with coordinates.
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('name');
$amountToSubtract = 0;
$beginningVerticalPosition = 480;
foreach ($products as $product)
{
$vert = $beginningVerticalPosition - $amountToSubtract;
$page->drawText($product['sku'], 40, $vert);
$page->drawText($product['name'], 150, $vert);
$amountToSubtract = $amountToSubtract + 13;
}
Then you have to render it as application/pdf instead of html. I'm not sure how you're using it so it's hard to tell what the best way would be.
wkhtmltopdf is a better option. You can take screenshots of your website with it, one way is with a new store view that only your tool uses, this can then have higher resolution images in the thumbnails, svg version of the logo and the buttons hidden.

Resources