MPDF - fseek error - php

I am trying to merge files together into one pdf. The files could be pdf, png or jpg files. Images work fine, its only when I try to export the pdf files that I get an error. The error is show below.
Message: fseek(): stream does not support seeking
I thought I found a solution from various forums relating to the path of the file. However, if I change the path it shows this error
mPDF error: Cannot open http://192.168.2.35/marine/certificate_files/a025ad3d40b22ac760ba7af7b6bb259d.pdf
My controller code is below
include('mpdf/mpdf.php');
$mpdf=new mPDF();
$mpdf->SetImportUse();
$mpdf->SetFooter($personnel_data->firstname . ' ' . $personnel_data->lastname . '|{PAGENO}|' . $personnel_data->ID_number );
foreach($certificate_data as $certificates)
{
$certificate_extension['type'] = explode('.',$certificates->certificate_name);
if($certificate_extension['type'][1]==='pdf')
{
$pagecount = #$mpdf->SetSourceFile('http://192.168.2.35/marine/certificate_files/' . $certificates->certificate_name);
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->WriteHTML('<pagebreak>');
}
if($certificate_extension['type'][1]!=='pdf')
{
$mpdf->WriteHTML('<p><img src="' . $this->config->base_url('assets/images/header-logo.png') . ' "></p>');
$mpdf->WriteHTML('<style>body {font-family: arial;}</style>');
$mpdf->WriteHTML('<p> ' . $certificates->certificate . ' - ' . $certificates->expiry_dates . '</p>');
$mpdf->WriteHTML('<p><img src="' . $this->config->base_url('certificate_files/' . $certificates->certificate_name) . ' "></p>');
$mpdf->WriteHTML('<pagebreak>');
}
else
{
$mpdf->WriteHTML('');
}
}
$mpdf->Output();
exit;
If any help or guidance could slide my way form anyone I would be forever grateful! Thanks!

as far as i know mpdf is based on fpdf (fpdi)... from the FPDI website: http://www.setasign.de/products/pdf-php-solutions/fpdi-pdf-parser/
By default FPDI can "only" handle PDF documents up to PDF version 1.4.
Beginning with PDF version 1.5 there were new compression features
introduced which involve internal structure changes how a PDF document
can be created.
Please try it with an PDF Version <= 1.4 for testing...
in my case it was the reason and I bought a developer license for the commercial pfdi pdf parser for 100€

Related

How to generate thumbnail from PDF upload in laravel

I am trying to generate a thumbnail of the PDF I upload in laravel the thumbnail should be the first page of the PDF. Right now I am manually uploading an image to make the thumbnail like this:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
}
if (request()->has('cover')) {
$coveruploaded = request()->file('cover');
$covername = $request->book_name . time() . '.' . $coveruploaded->getClientOriginalExtension();
$coverpath = public_path('/uploads/cover');
$coveruploaded->move($coverpath, $covername);
$book->card_image = '/uploads/cover/' . $covername;
}
This can be tedious while entering many data I want to generate thumbnail automatically. I searched many answers but I am not able to find laravel specific. I tried to use ImageMagic and Ghost script but I couldn't find a solution and proper role to implement.
Sorry, can't comment yet!
You can use spatie/pdf-to-image to parse the first page as image when file is uploaded and store it in your storage and save the link in your database.
First you need to have php-imagick and ghostscript installed and configured. For issues with ghostscript installation you can refer this. Then add the package composer require spatie/pdf-to-image.
As per your code sample:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
$pdfO = new Spatie\PdfToImage\Pdf($pdfpath . '/' . $pdfname);
$thumbnailPath = public_path('/uploads/thumbnails');
$thumbnail = $pdfO->setPage(1)
->setOutputFormat('png')
->saveImage($thumbnailPath . '/' . 'YourFileName.png');
// This is where you save the cover path to your database.
}

mPDF not generating PDF in firefox

I am using mPDF library for creating PDF using html data.it works fine in google chrome,but in firefox it displays nothing.No files downloaded and no errors in console.
Here is my code :
require_once( $destination_path."/mpdf/mpdf.php");
$target_path = $destination_path . '/uploads/reports/';
$htmlData='<span>Sample Html Content</span>';
try {
$pdf= new mPDF();
$pdf->debug = true;
$pdf->SetFooter("MyApp" . '|{PAGENO}|' . date(DATE_RFC822));
$pdf->WriteHTML($htmlData);
$pdf->Output($fileOutputPath, 'F');
}catch (Exception $e) {
echo 'Caught exception: ', $e, "\n";
}
i am tried ob_clean() and headers for solve the issue,but it remains same .
Anyone knows how to solve this issue?

phpexcel not reading sheet correctly

I am using phpexcel 1.8 to read a .xlsx file with 92 rows. But I am getting only one row as the result. Then I opened the file & made some edits using libre office & then I tried to save the file & an option came up "Use Microsoft Excel 2007/2010/2013 XML Format" & I clicked on it & saved the file.
Then onwards I am getting correct results from reading the file using phpexcel. What could be wrong ? Any help would be appreciated.
Code I used is:
$inputFileName = 'test.xlsx';
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
$highRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highColumn = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
Thanks in advance!

Issue with mpdf while attachment

$html = $this->load->view('pdf_output_order_details', $pdf, true);
$pdfFilePath = $pdf['data'][0]->first_name . "_" . $pdf['data'][0]->last_name . ".pdf";
ini_set('error_reporting', E_STRICT);
$this->pdf = $this->m_pdf->load('A4-L');
$this->pdf->WriteHTML($html);
$this->pdf->Output($pdfFilePath, "F");
While the pdf file is creating successfully if i change to "F" to "D"
But when attchment comes into picture then it throws an error....
"mPDF error: Unable to create output file: abc.pdf"....
I have set all permission to mpdf lib folder and n number of things done but still it won't work.... Please help guys....
Thank You....
I think your $pdfFilepath should containg not only filename, but filepath too.
From mPdf documentation:
F: save to a local file with the name given by filename (may include a
path).
Try this
$pdfFilePath = $_SERVER['DOCUMENT_ROOT'] . '/files/' . $pdf['data'][0]->first_name . "_" . $pdf['data'][0]->last_name . ".pdf";
Of course make sure you have write access to the files folder.

Caching images using PHP Image Cache

I am using this image caching library (http://nielse63.github.io/php-image-cache/) to cache images I am getting using the OMBD API (http://omdbapi.com/).
This is my setup:
$imagecache = new ImageCache();
$imagecache->cached_image_directory = '/img/cache/';
$imagecache->cached_image_url = '/img/cache';
$cached_src_two = $imagecache->cache( 'http://image.tmdb.org/t/p/original/eYVZP9CibJg1fyZnZahfFIYqfPr.jpg' );
echo 'Original file size: ' . filesize($imagecache->image_src) . ' bytes<br>';
echo 'PHPImageCach-ified file size: ' . filesize($imagecache->cached_filename) . ' bytes<br>';
echo 'Total image size reduction: ' . (((filesize($imagecache->image_src) - filesize($imagecache->cached_filename)) / filesize($imagecache->image_src))*100) . '%';
echo '<img src="'.$cached_src_two.'" alt="">';
I am not getting any errors. But something weird is going on I can't understand. It is saving a local copy of the image in $imagecache->cached_image_directory - this is not compressed or cached. Then it is creating another file in the img directory.
I assume this is the cached file, but I am unable to open it to check. The cached filename looks like: eYVZP9CibJg1fyZnZahfFIYqfPr.jpg.jpeg
Note the jpg.jpeg. Even fixing the file extension does not work. I can't find anything useful in the documentation. What is going wrong here?

Categories