PHP Spreadsheet - Header image - php

I am doing a project in which I use the PHP Spreadsheet library, more info here.
I need to add an image, a jpeg file, in the header of the .xlsx document. For this I am using part of the example code from the official documentation. In this way:
// Create a New Excel
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setPath('app/files/images/example.jpeg');
$drawing->setHeight(36);
$spreadsheet->getActiveSheet()->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);
At the moment the image does not appear and I have not found any error in the code that I use or in the server log, it is a copy of the original, so the doubts that arise are if there is an incompatibility issue or something.
UPDATE:
I have done a test. With this code I have added an image in a cell:
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('app/files/images/example.jpeg'); /* put your path and image here */
$drawing->setCoordinates('A1');
$drawing->setWorksheet($spreadsheet->getActiveSheet());
It's works fine to use in cell because it's not works in header :(
Please Could you help me?

SOLUTION:
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setPath('app/files/images/example.jpeg');
$drawing->setHeight(25);
$sheet->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_RIGHT);
// Set header
$sheet->getHeaderFooter()->setOddHeader('&L&"Verdana,Negrita"&8HEADER TEXT&R&G');
After it sets the image object they is to set in OddHeader using codes:
$R -> To right align
$G -> Insert image
More info here.

Related

Add remote images to PhpSpreadseet cell

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

Save barcode in database

I was wondering if I could save bar code in database.
I am using "Code 39" type of barcode. Barcode will be generated from variable. I wanted to save it to database if possible. I have downloaded library from "http://www.barcodebakery.com". Can anyone give me suggestion how to do so?
Code works fine it displays the image, later on I will use bar code scanner to look for products, each of them will contain bar code.
<?php
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
require('class/BCGcode39.barcode.php');
$font = new BCGFontFile('font/Arial.ttf', 18);
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
$barcode="3RC402A00";
// Barcode Part
$code = new BCGcode39();
$code->setScale(2);
$code->setThickness(30);
$code->setForegroundColor($color_black);
$code->setBackgroundColor($color_white);
$code->setFont($font);
$code->parse($barcode);
// Drawing Part
$drawing = new BCGDrawing('', $color_white);
$drawing->setBarcode($code);
$drawing->draw();
header('Content-Type: image/png');
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
?>
Any suggestion is appreciated.
Thanks
You can
Save the actual code (string) in the database, and recreate the generation code on demand. This is the preferred method.
You can save the image to your system and link to the image file with a location (path) in your database.
You can save the image as a BLOB field in your database.
You might be able to serialize the $drawing object, and recreate this afterwards. Don't do this.
So bottomline: save your string to the database, but if you really can't recreate the code on demand: save the image and link to that.
$drawing = new BCGDrawing('yourfilename.png', $color_white);
That would save the image to a file.
To save a file into database, refer to this stackoverflow post.
How can I store and retrieve images from a MySQL database using PHP?

barcode39 generation in php

I have gone through this link for the barcode generation.
I have downloaded the zip file and tested in my local host. It worked there, after that I have kept the files in my project and checked there. It doesn't work instead it gave me corrupted image as below.
GIF87aP���������,P���������p�قzu���b�a�I���xmZơFÌ,��}r�~���[�pH��(��>V�ie����q����dZΰ�+�����'�=]��n9?^wv�g��W���UƵ���h�8x(h�����GG� I��z�I:�j�y*zY�ij�::[� �Z+p+�J�k�Z�K�k�|��L�)����`N���qt���r���"�P���>�NA<~��]�p��{���!D +Z�ؠ;
Now I've created a sample file in the same location in my project with simple <?php ?> tags and kept the code
include "Barcode39.php";
// set Barcode39 object
$bc = new Barcode39("abc");
// display new barcode
$bc->draw();
again it worked in the sample file which I have kept in the project, but it is not working in the project related file.
Not getting the issue here.
Its perfectly working fine, make sure Barcode39.php in the same location.
As well it required PHP:GD, check again its install or not.
(Include could be issue if you are using any other framework)
I tested and got the below output
You can also use the below code to generate the gif file of barcode and link it anywhere in any page. you will not face any issue related to using html tags.
<?
// include Barcode39 class
include "Barcode39.php";
// set object
$bc = new Barcode39("123-ABC");
// set text size
$bc->barcode_text_size = 5;
// set barcode bar thickness (thick bars)
$bc->barcode_bar_thick = 4;
// set barcode bar thickness (thin bars)
$bc->barcode_bar_thin = 2;
// save barcode GIF file
$bc->draw("barcode1.gif");
?>
and include this file in the src attr of the image tag like
<img src="http://localhost/yourbarcodefilepath" alt="barcode"/>
then it will work

JpGraph small demo is not working

I'm going to use JpGraph in my web-project. To do this, I downloaded the library into my project folder and created the following test code:
<?php
include ( "jpgraph-3.5.0b1/src/jpgraph.php");
include ("jpgraph-3.5.0b1/src/jpgraph_gantt.php");
// A new graph with automatic size
$graph = new GanttGraph (0,0, "auto");
// A new activity on row '0'
$activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");
$graph->Add( $activity);
// Display the Gantt chart
$graph->Stroke();
?>
The error message is:
The image “http://localhost:10088/test/check.php” cannot be displayed,
because it contains errors.
What's wrong with my code? In fact, it was just copied from here. The only thing I changed was the path to the JpGraph library. So, do I need to import this library in some special way into my project (so far, I've just copied the JpGraph's folder into the project's folder)? I'm using Zend Studio.
This can be caused by extra whitespace on the page. Make sure you don't have any spaces or line breaks before your opening <?php tag.

Error in generated pdf file using zend_pdf under Magento

I'm trying to create a PDF file, under a Magento phtml file, this is my code :
$pdf = new Zend_Pdf();
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$page=$pdf->pages[0]; // this will get reference to the first page.
$style = new Zend_Pdf_Style();
$style->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$style->setFont($font,12);
$page->setStyle($style);
$page->drawText('example text here',100,($page->getHeight()-100));
$pdf->render();
$pdf->save('test.pdf','true');
My PDF file is created, but I can't open it with acrobat reader.
When I open it with a text editor and compare it with another simple pdf files, I noticed that in the first line was missing in my generated pdf file. it contains "%PDF-1.4"
How can I add this line programmatically with zend_pdf in my pdf file ?
Thanks for help.
According to the zend manual the second save parameter is only for updating files that already exist. In this case you are creating a new file so don't use that option.
$pdf->save('test.pdf');
PS. This answer is technically an RTM statement.

Categories