I'm using Laravel Excel to dump a number of records to XLS format.
Each of my records has an image_url field that contains a remote URL to a JPG file.
I'd like to display these images in the final XLS file, in their own column.
I tried creating a Blade template for the export, but when using <img> tags, the images don't appear in the XLS file. I'm not getting any errors, and the documentation for Laravel Excel is very sparse when it comes to image integration.
So what would be the easiest way to get the images to display in the XLS, using Laravel Excel? Is there some way I can get the Blade template to work, or must I add the images more programmatically?
something like this should work, you gotta load the PHPExcel_Worksheet_Drawing library.
$filename = 'File Name';
Excel::create($filename, function($excel) {
$excel->sheet('sheet name', function($sheet) {
$objDrawing = new PHPExcel_Worksheet_Drawing;
$objDrawing->setPath(public_path('your/image/path.jpg')); //your public image path
$objDrawing->setPath(storage_path('your/image/path.jpg')); //your storage image path
$objDrawing->setCoordinates('A2');
$objDrawing->setWorksheet($sheet);
});
})->export('xls');
Related
There is a simple two-column table on a website: product name and product image. It's very easy to render in HTML.
The task would be creating an Xlsx file with these columns.
The images are not stored locally but all of them are remote images with full URL.
The export contains ~100-200 rows.
I tried to create a resource with imagecreatefromjpeg and adding it with MemoryDrawing but it took a huge amount of resources.
I tried with Html helper's toRichTextObject and a simple tag but got empty result.
How is it possible to add a remote image to a PhpSpreadsheet cell? It doesn't need working offline, it's fine to load the remote images when the file is opened.
As #FabriceFabiyi mentionned in his comment, and after reading:
PhpSpreadseet documentation on drawing.
PHP documentation on file_get_contents.
PHP documentation on file_set_contents.
This worked for me:
$image = file_get_contents('https://website.com/path/to/image');
$imageName = 'a_nice_image_name';
//You can save the image wherever you want
//I would highly recommand using a temporary directory
$temp_image=tempnam(sys_get_temp_dir(), $imageName);
file_put_contents($temp_image, $image);
// And then PhpSpreadsheet acts just like it would do with a local image
$drawing->setPath($temp_image);
$drawing->setHeight(36);
$drawing->setWorksheet($sheet);
$drawing->setCoordinates('B15');
More on temporary directories in the PHP docs:
sys_get_temp_dir()
tempnam()
There is an example how add image to your excel export:
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('/images/image-1.png'); // put your path and image here
$drawing->setHeight(30);
$drawing->setCoordinates('A5');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
Specifying coordinates for the image might help, as per the examples and the documentation
$objDrawing->setCoordinates('A3');
Note that an image isn't in a cell/column/row, but overlaid over the main sheet at the same position as that cell/column/row
I am working as a freelancer and one of my clients asked me to create designed xls file for a Laravel project.
Using Laravel and Maatawebsite for xls files I have generated a xls file using this code:
Excel::create('extract', function ($excel) use ($data) {
$excel->sheet('mySheet', function ($sheet) use ($data) {
$sheet->fromArray($data);
});
})->download('xls');
This function is generating a xls file like this one:
Generated xls file
It is possible to generate something designed like example from bellow using PHP or Laravel?
How xls file should look like
Of course it is possible, but you'd have to do alot of the styling stuff in your code.
There are a couple of good Excel libraries for PHP available, f.e.
https://github.com/PHPOffice/PhpSpreadsheet
I implemented this library to generate barcodes images in my laravel project https://github.com/milon/barcode
For generating the excel sheet i use this library :
https://github.com/Maatwebsite/Laravel-Excel
For the barcode image i just add a value in my database and then i convert that value in a barcode image and know i need to export the barcode in excel
For exporting the barcode i tried many things but none of them seems to work
in my view i tried different methods for exporting the barcode
The first method :
<td><img src="data:storage/app/upload/;base64,{!! DNS1D::getBarcodePNG("$employee->barcode", 'C39')!!}" ></td>
and gives me the following error:
(1/1) PHPExcel_Exception File
data:storage/app/upload/;base64,iVBORw0KGgoAAAANSUhEUgAAAcAAAAAeAQMAAACBqFzbAAAABlBMVEX///8AAABVwtN
AAAAAXRSTlMAQObYZgAAACpJREFUOI1jOHD
/Jk/B878OcN//gyxGKSHYVTjqMZRjaMaRzWOaqSXRgC0qVOpA6XqWgAAAABJRU5ErkJggg==
not found!
when i use this code :
<td>{!! DNS1D::getBarcodeHTML("$employee->barcode", "C39")!!}</td>
it gives me the following error:
This site can’t be reached
The webpage at
http://127.0.0.1:8000/admin/employees/J80302041D/more/excel might be
temporarily down or it may have moved permanently to a new web
address. ERR_INVALID_RESPONSE
the controller that i use to export the barcode:
public function excel($nr_personal,Request $request)
{
$employees = Employee::where('nr_personal',$nr_personal)->get();
$worker = Employee::where('nr_personal',$nr_personal)->first();
Excel::create('Employee Excel', function($excel) use ($employees, $worker) {
$excel->sheet('Excel sheet', function($sheet) use ( $employees, $worker) {
$sheet->loadView('admin.employees.excel')->with('employees', $employees)
->with('worker', $worker);
$sheet->setOrientation('landscape');
});
})->export('xls');
}
How can i export the barcode in a excel sheet??
This is my sample code.
I'm stuck on how to insert images into this.
I would be grateful for any help:
include('autoload.php');
use mikehaertl\pdftk\Pdf;
$dat = "review.png";
$pdf = new Pdf('Josco.pdf');
$pdf->fillForm(['signature'=>$dat ]);
$pdf->saveAs('my.pdf');
I don't know how to fill image in image type field. Please find attached
the pdf file for reference :
[1]: https://drive.google.com/file/d/1wz_mhdCFbG5o_1CgF6VlIJ4Gh70Ne8-E/view?usp=sharing
It is not possible to add image in a pdf using pdftk alone. A possibility would be to use tcpdf or fpdf to generate a pdf file of a blank page with the signature located where you want. Then to use the pdftk stamp function to stamp this generated file on the source pdf
I am creating .docx files from a template using PHPWord. It works fine but now I want to convert the generated file to PDF.
First I tried using tcpdf in combination with PHPWord
$wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx");
\PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf");
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
if (file_exists($filename.".pdf")) unlink($filename.".pdf");
$pdfWriter->save($filename.".pdf");
but when I try to load the file to convert it to PDF I get the following exception while loading the file
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'
After some research I found that some others also have this bug (phpWord - Cannot add PreserveText in Section)
EDIT
After trying around some more I found out, that the Exception only occurs when I have some mail merge fields in my document. Once I removed them the Exception does not come up anymore, but the converted PDF files look horrible. All style information are gone and I can't use the result, so the need for an alternative stays.
I thought about using another way to generate the PDF, but I could only find 4 ways:
Using OpenOffice - Impossible as I cannot install any software on the Server. Also going the way mentioned here did not work either as my hoster (Strato) uses SunOS as the OS and this needs Linux
Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
Using PHPLiveDocx - This works, but has the limitation of 250 documents per day and 20 per hour and I have to convert arround 300 documents at once, maybe even multiple times a day
Using PHP-Digital-Format-Convert - The output looks better than with PHPWord and tcpdf, but still not usable as images are missing, and most (not all!) of the styles
Is there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?
I used Gears/pdf to convert the docx file generated by phpword to PDF:
$success = Gears\Pdf::convert(
'file_path/file_name.docx',
'file_path/file_name.pdf');
You're trying to unlink the PDF file before saving it, and you have also to unlink the DOCX document, not the PDF one.
Try this.
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
$pdfWriter->save($filename.".pdf");
unlink($wordPdf);
I don't think I'm correct..
You save the document as HTML content
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
After than you read the HTML file content and write the content as PDF file with the help of mPDF or tcPdf or fpdf.
Try this:
// get the name of the input PDF
$inputFile = "C:\\PHP\\Test1.docx";
// get the name of the output MS-WORD file
$outputFile = "C:\\PHP\\Test1.pdf";
try
{
$oLoader = new COM("easyPDF.Loader.8");
$oPrinter = $oLoader->LoadObject("easyPDF.Printer.8");
$oPrintJob = $oPrinter->PrintJob;
$oPrintJob->PrintOut ($inputFile, $outputFile);
print "Success";
}
catch(com_exception $e)
{
Print "error code".$e->getcode(). "\n";
print $e->getMessage();
}