Convert multi image/pdf to one single pdf file - php

i want to combined multi image/pdf to one single pdf file in laravel. i use Imagick packages, here is my code:
$document1 = DocCust::latest('created_at')->where('cus_id',$id)->where('type','1')->first();
$document2 = DocCust::latest('created_at')->where('cus_id',$id)->where('type','2')->first();
$file1= public_path().'/documents/user_doc/'.$id.'/'.$document1->doc_pdf;
$file2= public_path().'/documents/user_doc/'.$id.'/'.$document2->doc_pdf;
$path = public_path().'/documents/user_doc/'.$id;
$images = array($file1, $file2);
$pdf = new Imagick($images);
$pdf->setImageFormat('pdf');
$pdf->writeImages('combined.pdf', true);
its works perfectly, but the new file pdf is saved to 'public' folder. i want to save the new pdf into folder based on their id.
public/documents/user_doc/'.$id.'
i tried to change this code with path, but i get error failed to open stream not found file etc..
$pdf->writeImages($path.'/'.'combined.pdf', true);

Related

How to translate the content of a PDF file and then replace it with the translated content?

I know how to use the Google Translate API. I want to know one thing do you have an idea to translate the PDF file without losing in the file format. I had tried to convert the PDF file to DOCX and then translate the file and then return it to PDF but the conversion from PDF to DOCX failed because of many PHPWORD BUGs.
Here is the question and the codes that I had received in order to request the conversion from PDF to DOCX.
<?php
require_once 'vendor/autoload.php';
// Create a new PDF reader
$reader = \PhpOffice\PhpWord\IOFactory::createReader('PDF');
$reader->setReadDataOnly(true);
// Load the PDF file
$phpWord = $reader->load('example.pdf');
// Save the DOCX file
$writer = $PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
$writer->save('example.docx');
echo 'PDF file converted to DOCX successfully!';
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory as WordIOFactory;
// Convert PDF to text
exec('pdftotext -layout input.pdf output.txt');
// Load text file
$text = file_get_contents('output.txt');
// Create new DOCX file
$phpWord = new \PhpOffice\PhpWord();
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText($text);
// Save DOCX file
$objWriter = WordIOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('output.docx');
Do you have any idea how I can translate the PDF file without going through the conversion to other formats?

Convert the first n pages of a pdf file to a single png Image file using PHP ImageMagick library

I'm working with the PHP ImageMagick Library to convert uploaded pdf files to single png files so I can display the pdf as a single image on my Laravel webpage. So far I'm able to convert an entire pdf to a single image with this code:
<?php
$imagick = new Imagick();
$file = new File;
// other lines of code
// ...
$imgPath = Storage::path($file->file_path);
$imgSavePath = Storage::path('uploads/buffer/'.Str::beforeLast($file->name, '.').'.png');
$imagick->readImage($imgPath);
$imagick->resetIterator();
$imagick = $imagick->appendImages(true);
$imagick->writeImages($imgSavePath, true);
This works by producing a single png image. However, I find this to be resource intensive (storage wise) and time consuming because I'm delivering the functionality through an ajax call.
I want my web application to convert only the first n pages of the pdf (say first 5 pages) into a single image to act as a preview on the site - thereafter the user can download the entire pdf to view on their local system. The function should work regardless of the number of pages in an uploaded pdf document.
So far, I only find in the documentation where I can read a page at a particular index from the Imagick object and convert to an image using:
...
$imgPath = Storage::path($file->file_path);
$index = 5;
$imagick->readImage($imgPath. '[' . $index . ']');
However, I'm finding it difficult to refactor this so that the application can read the first n pages.
Intuitively, the readImage() function seems to work in a similar way as the command line syntax. Thanks to the hint from #MarkSetchell in the comments:
<?php
$imagick = new Imagick();
$file = new File;
// other lines of code
// ...
$imgPath = Storage::path($file->file_path);
$imgSavePath = Storage::path('uploads/buffer/'.Str::beforeLast($file->name, '.').'.png');
$imagick->readImage($imgPath.'[0-4]'); // read only the first 5 pages
$imagick->resetIterator();
$imagick = $imagick->appendImages(true);
$imagick->writeImages($imgSavePath, true);
I'm using ImageMagick 6.9.10-68 and PHP 8.1.12

How to download and store and then upload dropbox shared image in laravel?

I am trying to write a script to upload bulk product from excel sheet which contain product name, price and images. Images are the dropbox image share link. How can I download those image from the dropbox url, save them on my server, then upload the image url to my database?
Excel sheet reading:Maatwebsite/Laravel-Excel
Generic upload code:
public function productUpload(Request $request){
if($request->hasFile('products')){
$path= $request->file('products')->getRealPath();
$data = Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
//download the image create thumbain and store to /images/product/thumbnail folder and get the link,
$thumbnail = //here will be the path for the thumbnail
//Original image
$original = ;
$data['original']= $original;
$data['thumbnail']=$thumbnail;
$data['name']=$value->name;
$data['price']= $value->price;
Product::create($data);
}
return redirect()->back()->with('success','Product has been uploaded');
}
}
}
The image url that is in the excel sheet is like this one https://www.dropbox.com/s/x2tbsy49sraywvv/Ruby12.jpg?dl=0 This file has been removed at this point.
You can dowload the image directly by adding dl=1 to the dropbox link Source
And you can dowload the image to the server with the file_get_contents php command. Source and store it in the server with the Storage Facade
$url = "yoururl"; //check if has the dl=0 and change it to 1 or add dl=1
$contents = file_get_contents($url);
$name = 'image.png';//the name of the image to store on disk.
\Storage::disk('yourdisk')->put($name, $contents);
Also you will get the full path to the image like this.
$thumbnail = storage_path('yourdir/'.$name)
Tested it with a file that i have on my dropbox and worked without a problem.
You still need to handle some things like errors and delays while dowloading the images.

Qr-Code using Bacon/BaconQrCode need to save to the folder.

I am using Bacon/BaconQrCode library to generate Qr-Code image so how can
save the Qr-code images to the specific folder ?? please help.
My Code:-
$renderer = new \BaconQrCode\Renderer\Image\Png();
$renderer->setHeight(256);
$renderer->setWidth(256);
$writer = new \BaconQrCode\Writer($renderer);
$writer->writeFile('www.google.com', 'qrcode.png');
How can I save this Qr-code image to the folder to show or fetch from other location.
The \BaconQrCode\Writer->writeFile uses file_put_contents method so you can just add an absolute path to your directory like : $writer->writeFile('www.google.com', '../public/qrcodes/qrcode.png');
I haven't tried yet but i hope it works for you.

extract kmz file with php

Hi I want to extract a KMZ file that has an Images folder with some images and a kml file
Other questions here just have the kml file but mine has a images folder too!
how can I extract it?
I tried this but it didn't work
$data = file_get_contents("test/test.kmz"); // url of the KMZ file
file_put_contents("/test/kmz_temp",$data);
ob_start();
passthru('unzip -p /test/kmz_temp');
$xml_data = ob_get_clean();
header("Content-type: text/xml");
dd($xml_data);
and also this
$zip = new ZipArchive();
$zip->open('test/test.kmz');
$zip->extractTo('/test/public');
$zip->close();**strong text**
I found my solution
if you change the format of the file (while moving it to your folder) to zip it will extract it

Categories