php image save to dir and save to pdf - php

Im using http://www.ashberg.de/php-barcode/ scripts to generate a barcode. I now need to import the barcode into a PDF from html. (Im using html2pdf_v4.03 for html -> pdf)
<div style=" display:inline;"><img id='barcode' alt='barcode' src="scripts/barcode/barcode.php?mode=jpg" /></div>
works, but when I import it to PDF it errors. So I decided to try generate the barcode and save it to a dir as an img and refrence it in the html I then convert to PDF.
So I tried
file_put_contents('scripts/barcode.jpg', file_get_contents('scripts/barcode/barcode.php?mode=jpg'));
and I get failed to open stream: error
if i try
file_put_contents('scripts/barcode.jpg', file_get_contents('scripts/barcode/barcode.php'));
It runs but the image file it creates is not readable/ broken.
Please assist?

Write full url (http://domain.com/scripts/barcode/barcode.php) instead of scripts/barcode/barcode.php. Probably its opens local php file when you use file_get_contents()

Related

HTML / PHP parse a file from memory to src

I call a PDF creator class FPDF through AJAX to create a PDF doc which I finally need to display as a preview in a separate browser window. I know that I can use this element:
echo '<embed src="'.$complete_path.'" type="application/pdf" width="100%" height="'.MAX_HEIGHT.'px">';
But src refers to a filepath on disk as far as I know. I want to avoid storing a PDF file first on disk while its already created in memory. So my question is if there is a way to parse a given memory buffer to src and how to do that ?
If you are calling it via AJAX then the best way to do it is as suggested in comments, create a temporary file in server and return the URL to the client-side because you will need to do two requests, the AJAX request and another from the embed tag.
Even though, I think we are forgetting how HTTP works here. You can return a PDF file from a URL. Simply create an embed tag and set the src attribute to a script that will simply Output the contents of your FPDF object. Like this.
echo '<embed src="/path/to/script.php" type="application/pdf" width="100%" height="'.MAX_HEIGHT.'px">';
And in the /path/to/script.php file you would put the PDF generation logic.
// Create FPDF instance, write content, ...
$FPDF->Output();

Storing Images In a PHP File

I have been trying to change the images in the WampServer Index.php File... When I looked at the Index.php File I saw something Interesting. The File its self was containing the Image in the RAW format. Latter in the Code the PHP Script Calls the Image using the URL like http://localhost/index.php?img=pngFolder called a Image file stored RAW in the PHP file as a png.
Here is a link to a website that has the index.php code...
Link
I would Like to know how to replicate this same process to work for other images. Granted the File will be larger but its a Price to pay for what I am doing for a project. The Reason I want some help with this is so I can do it correctly. I have Tried 2 times already. I managed to get it to call one image correctly but not some of the others. I'm not sure if the image is just a different encoding or what..... Any Help would be Appreciated.
They are using BASE64 to encode the images into text. You can google for a base64 encoder that will convert your images to text. You can then put the text directly in an <img src="..base64 text.." />
Here's one..
https://www.base64-image.de/
As far as getting the image from the url index.php?img=pngfolder..
You could put this at the top of the file
if(isset($_GET['img'])){
echo "...base64 string.."; exit;
}
Then you can use the index url as the src for your image and it will simply retrieve the base64 image

print image on odt file using php

I have a php page which outputs an .odt file printing data retrieved from a database.
I use the method below to create the .odt file:
header('Content-Type: application/vnd.oasis.opendocument.text');
The only problem I am experiencing is with <img> tag. The following command is ignored and no pictures appear on my .odt file:
<img src="/path_to_images/my_image.png">
I have tried using both the relative and the absolute paths to point to the image.
The answer suggested by uri2x worked just fine.
In conclusion, the full URL enables the image to appear on the .odt file:
<img src="http://example.com/path/image.png">

prevent automatic file download and display file in php form using iframe

I need to display doc or docx or pdf kind of file in php code.Here is my code,
<iframe src="http://localhost/sample/sampl1/resource/upload/resumes/1406263145_resume sample.docx"
style="width:820px; height:700px;" frameborder="0"></iframe>
in localhost...
When i load the form,the given docx file is downloading automatically and the file is not view in a php form...So any one can help me?
To view document files (doc, docx, etc), you can use either the Zoho Viewer or Google Docs Viewer jQuery plugin.

HTML2PDF generates corrupted file

I'm using HTML2PDF (http://html2pdf.fr/) to generate PDF files from a webpage. However, I'm having a kind of weird error: the generated file can be perfectly shown with the "Preview" Mac tool (or simply touching the space bar when the downloaded PDF file is selected), but when I try to open the file with the Adobe Acrobat Pro, the following error message appears:
Adobe reader could not open "file.pdf" because it is either not a support file type or because the file has been damaged (for example, it was sent as email attachment and was'nt correctly decoded).
Here is how I'm generating the PDF content and the PDF file itself:
$content = '<page backtop="12mm" backbottom="12mm" backleft="8mm" backright="8mm" style="font-size: 10pt;">
<h1>MyTitle</h1>
<br>
<table style="width: 100%;">
<tr>
<td style="width: 25%;"></td>
....
</tr>
</table>
</page>';
$html2pdf = Yii::app()->ePdf->HTML2PDF('P','A4','en',true,'utf-8');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($content);
$html2pdf->Output($outputname , EYiiPdf::OUTPUT_TO_DOWNLOAD );
Any idea on what can be happening? Maybe I'm missing something about the format or the file encoding? I've tried different encodings but nothing seems to solve the problem...
Thanks in advance for your time and effort! :)
Solved! After dealing with this a little bit more, I've decided to open the PDF file with a plain text editor, and I've found that the PHP page used to generate the file included by default a header and a footer (I'm using the Yii Framework), which resulted in "printing" the HTML header and footer code in the PDF file... Is anyone ever has the same problem I had, the way to solve it is to open the page's controller file (in 'protected/controllers/myPageController.php') and change the following line:
$this->render('index');
By this one:
$this->renderPartial('index');
Everything works as expected now! ^_^

Categories