I have to generate Pdf of images. I have tried the following code
public function getPdf($paths)
{
try {
$pdf = new Zend_Pdf();
for($i=0; $i<5; $i++)
{
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$image = Zend_Pdf_Image::imageWithPath($paths[$i]);
$page->drawImage($image, 20, 20, 50, 46);
$pdf->pages[] = $page;
}
$pdf->save('document.pdf');
echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
die ('PDF error: ' . $e->getMessage());
} catch (Exception $e) {
die ('Application error: ' . $e->getMessage());
}
}
Please note that $paths is an array which contains correct physical paths like C:/wamp/www/magento1.7/media/catalog/product/cache/1/small_image/9df78eab33525d08d6e5fb8d27136e95/p/o/portf1.jpg.... Now the problem is that it is not able to create Pdf and throws exception saying "PDF error: Cannot create image resource. File not found". Can anybody helps me in this or simply point out what's wrong with this code.
You probably do not have required permissions. Look if file is_readable and if not try chmod it.
You can also always change file permissions on your windows.
Edit1: Change your code:
$numPaths = count($paths);
for($i=0; $i < $numPaths; $i++) {
..
Related
I want to convert an Excel file to PDF.
Here is my code:
function excel() {
$excel = new COM("Excel.Application") or die ("ERROR: Unable to instantaniate COM!\r\n");
$file = base_url('archieve/ADAF - 2018000007.xlsx');
$Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!\r\n");
$Worksheet = $Workbook->Worksheets(1);
echo 'file loaded';
$xlTypePDF = 0;
$xlQualityStandard = 0;
try {
$Worksheet->ExportAsFixedFormat($xlTypePDF, base_url('archieve/ADAF test.pdf'), $xlQualityStandard);
} catch(com_exception $e) {
echo $e->getMessage()."\n";
exit;
}
echo 'file convert';
$excel = NULL;
unset($excel);
}
When I run the function, the server is still loading and reaches timeout. The code is in a different server.
Is my code wrong? Since I have tested in local PC and it works.
I tried to run the COM using Python and it worked fine. Turns out there is a problem in PHP when export as pdf. I still have the issue. Any suggestions?
I am splitting PDF file and after split I want to read that split page text but that is returning me nothing. If I am reading before splitting is is working fine and returning me all PDF text but don't know why it is not working after splitting. Here is my code :-
<?php
function split_pdf($filename, $end_directory = false)
{
require_once('../fpdf/fpdf.php');
require_once('../fpdi/fpdi.php');
require_once('../pdf2text/class.pdf2text.php');
$end_directory = $end_directory ? $end_directory : './';
$new_path = preg_replace('/[\/]+/', '/', $end_directory.'/'.substr($filename, 0, strrpos($filename, '/')));
if (!is_dir($new_path))
{
// Will make directories under end directory that don't exist
// Provided that end directory exists and has the right permissions
mkdir($new_path, 0777, true);
}
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($filename); // How many pages?
// Split each page into a new PDF
for ($i = 1; $i <= $pagecount; $i++) {
$new_pdf = new FPDI();
$new_pdf->AddPage();
$new_pdf->setSourceFile($filename);
$new_pdf->useTemplate($new_pdf->importPage($i));
try {
$new_filename = $end_directory.str_replace('.pdf', '', $filename).'_'.$i.".pdf";
$new_pdf->Output($new_filename, "F");
echo "Page ".$i." split into ".$new_filename."<br />\n";
$pdf2text = new PDF2Text();
$pdf2text->setFilename($end_directory.$filename);
$pdf2text->decodePDF();
$text = $pdf2text->output();
echo "This is page text =====>> ".$text."<br><br>";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
}
// Create and check permissions on end directory!
split_pdf("test.pdf", 'split/');
?>
Please help me I don't have any idea why is this happening?
i was splitting pdf into different single page using fpdf and fpdi. Everything works fine but the link inside pdf was not working. Link was removed on splitted single pages.
split_pdf("test.pdf", 'splitedpdf/');
function split_pdf($filename, $end_directory = false)
{
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
$end_directory = $end_directory ? $end_directory : './';
$new_path = preg_replace('/[\/]+/', '/', $end_directory.'/'.substr($filename, 0, strrpos($filename, '/')));
if (!is_dir($new_path))
{
// Will make directories under end directory that don't exist
// Provided that end directory exists and has the right permissions
mkdir($new_path, 0777, true);
}
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($filename); // How many pages?
// Split each page into a new PDF
for ($i = 1; $i <= $pagecount; $i++) {
$new_pdf = new FPDI();
$new_pdf->AddPage();
$new_pdf->setSourceFile($filename);
$new_pdf->useTemplate($new_pdf->importPage($i));
try {
$new_filename = $end_directory.str_replace('.pdf', '', $filename).'_'.$i.".pdf";
$new_pdf->Output($new_filename, "F");
echo "Page ".$i." split into ".$new_filename."<br />\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
// $pdf->close();
}
FPDI is not able to handle any dynamic content link links, form fields or any other annotation type. There's an extension which support at least links (only compatible with FPDI 1.4.4 + FPDF_TPL 1.2.3).
If you need to extract the pages including all attached annotations, you may check out the SetaPDF-Merger component (not free!).
Below is the code that throws some errors while getting executed. What I'm trying to do is the last line of the code gets executed no matter what (Error or no Error).
<?php
require 'main.php';
function create_photo($file_path) {
# Upload the received image file to Cloudinary
#$result = \Cloudinary\Uploader::upload($file_path, array(
"tags" => "backend_photo_album",
));
#unlink($file_path);
error_log("Upload result: " . \PhotoAlbum\ret_var_dump($result));
$photo = \PhotoAlbum\create_photo_model($result);
return $result;
}
$files = $_FILES["files"];
$files = is_array($files) ? $files : array($files);
$files_data = array();
foreach ($files["tmp_name"] as $index => $value) {
array_push($files_data, create_photo($value));
}
?>
<script>window.location.replace('index.html')</script>
Any help would be much appreciated. Thanks
I think depending on your php version, you can use a "try/catch/finally" bloc like that:
try
{
// code that may throw an exception
}
catch(Exeption $e) // The exception you want to catch
{
// Exception treatment
}
finally
{
// Executed no matter what
}
Maybe take a look about how to use that.
I am using the PHP libraries TCPDF and FPDI to combine PDF documents, and am getting the following error:
TCPDF ERROR: Unable to find object (10, 0) at expected location
I have the commercial version of FPDI.
It appears that the issue is only happening with PDF Version 1.3 (Acrobat 4.x) files. Here is a screenshot of a file's document properties that is creating the error. http://imagebin.org/215041
I'd like to skip over any files with errors instead of letting the script die. I have modified the error handling with a new class ErrorIgnoringTCPDF, however, it is not working.
Any ideas?
require_once('../../libraries/tcpdf/tcpdf.php');
require_once('../../libraries/fpdi/fpdi.php');
class ErrorIgnoringTCPDF extends FPDI {
public function Error($msg) {
// unset all class variables
$this->_destroy(true);
// exit program and print error
//die('<strong>TCPDF ERROR: </strong>'.$msg);
}
}
$pdf = new ErrorIgnoringTCPDF();
$pdf->setPrintHeader(false);
$prows = fetch_data($id);
foreach ($prows AS $row) {
$irows = get_imaged_docs($row['pat_id']);
foreach($irows AS $irow){
if ($irow['type'] === 'application/pdf'){
$doc_id = $irow['id'];
$content = get_pdf_imaged_docs($doc_id);
$pagecount = $pdf->setSourceFile($content);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->ImportPage($i);
$s = $pdf->getTemplatesize($tplidx);
$pdf->AddPage('P', array($s['w'], $s['h']));
$pdf->useTemplate($tplidx);
}
} else {
$pdf->AddPage();
$doc = fetch_document_content($irow['id'], $irow['filename']);
$img = base64_encode($doc);
$imgdata = base64_decode($img);
$pdf->Image('#'.$imgdata);
}
}
}
$pdf->Output('documents.pdf', 'D');
If you are using Linux you can use shell_exec to combine files
function combine_pdf($outputName,$fileArray)
{
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";
foreach($fileArray as $file)
{
$cmd .= $file." ";
}
$result = shell_exec($cmd);
}
Have you tried just suppressing the error?
$pagecount = #$pdf->setSourceFile($content);
if (empty($pagecount))
continue; // or whatever you want to do, maybe set $is_invalid = true;
This simply indicates that the PDF document is errorious. It points to a specific byte offset position where the expected object is not found.
I wont say this is an appropriate/best fix, but it may resolve your problem,
In: pdf_parser.php, comment out the line:
$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
It should be near line 544.
You'll also likely need to replace:
if (!is_array($kids))
$this->error('Cannot find /Kids in current /Page-Dictionary');
with:
if (!is_array($kids)){
// $this->error('Cannot find /Kids in current /Page-Dictionary');
return;
}
in the fpdi_pdf_parser.php file
Hope that helps. It worked for me.
I have the same problem and i am using this code to fix my problems.
class convertPDF extends FPDI {
public function error($msg) {
throw new Exception($msg);
}
...other stuff...
}
try {
$convertPdf = new convertPDF();
} catch(Exception $e) {
die($e->getMessage);
}
This answer is for people who search for this problem. Have luck!