Generate PDF with base64 encode string - php

I am using an API which provides me base64 encoded content. It is mentioned that we can download the PDF file by saving the content to a .pdf file.
In the API documentation, it's clearly mentioned
A PDF that contains the contents of the invoice. You encode the
content with a Base64 string to create a Base64-encoded .pdf file.
You can query this field to download the invoice PDF for a specific
invoice. After you query it, you can save the string to a .pdf file,
then view the file in any PDF reader.
I am using TCPDF to write the content in PDF file. But it generates a blank PDF file.
$PDF = new PDF('Test Title', 'Test Subject');
$PDF->GetTCPDF()->write(0, 0, '', '',
base64_decode($this->Get(self::Body)), 0, 1, 0, true, '', true);
$PDF->Download();
Am I doing anything wrong?

I think you should use this code to get your pdf file :
$data = base64_decode($this->Get(self::Body));
file_put_contents('mypdf.pdf',$data);
Then you can open it.
Or you can echo the content to your page like this :
$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;
Good luck

The last time I get a blank PDF I forgot adding a new Page with AddPage().
Apart from this I think M2sh is right.

Related

Saving PDF file as "data" - using PHP MPDF

IM trying to save a PDF file, generated from HTML, into the user's local using MPDF. Here is the part of the code responsable for this:
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('gestao/relatorios/relatorio_cargo_cidades', $data, true);
$mpdf->WriteHTML($html);
$mpdf->SetFooter(relatorio_footer());
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
At first I was trying to show the PDF using the "I" param from the output function, than the user could just see the PDF and choose to download it or not. But when I tried to submit a file into another website, it says that the file is not a PDF. Than I used my linux to see if the file was actually a PDF, here's what I got:
As you can see, the file is being saved as "data" for some reason. I've already tried to use the 'F' param, also from the output function, and than it worked, I saved as PDF. But the F param only save the file inside the code folder, so it was not very usefull for me.
Can anyone tell me how could I save the file as an actually PDF using MPDF?
EDIT
I think that the problem is being caused by CODEIGNITER, not by the MPDF. When I'm setting the last param from the load->view as TRUE, the returned HTML is in a data (string) form, and MPDF is not converting this properly.
Adding ob_clean() might solve your problem:
ob_clean();
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
You can simply use the D parameter for downloading:
$mpdf -> Output ('FILENAME.pdf', 'D');
or generate your own header and then output the content as string:
header ("Content-type: application/pdf");
header ("Content-Disposition: inline; filename=FILENAME.pdf");
$mpdf -> Output ('', 'S');

Invalid characters when call output method with custom fonts in dompdf php

I use laravel dompdf to create PDF files. What I'm trying to do is create pdf file, upload it to storage and then download it. there's specific endpoint, when user call it all these actions happen.
// fetch data for pdf
$pdfData = $this->getData();
// creates pdf from html
$pdf = PDF::loadView('pdf.example', [
'items' => $pdfData
])->setPaper('a4', 'landscape');
// upload file to storage
Storage::put("/my-path", $pdf->output());
// download file
return $pdf->download("file.pdf");
In html(from which pdf's been created) I have custom fonts(load them with #font-face). the issue is that after calling output method $pdf->output() the characters in pdf are invalid
BUT, if I don't call $pdf->output() characters are shown properly(with my custom fonts). so the issue happens only when I use custom fonts and then call $pdf->output(). any idea how to fix it?
I already saw this solution but it doesn't help, also there's no load_font.php in this package

PHP QR Code scan from PDF file

I have a PDF file with a QR Code. Iuploaded the file on the server folder named "tmp" and now I need to scan and convert this QR via PHP.
I found this library:
include_once('lib/QrReader.php');
$qrcode = new QrReader('/var/tmp/qrsample.png');
$text = $qrcode->text(); //return decoded text from QR Code
print $text;
But this works only for png/jpeg files.
Is there any way to scan PDF ? Is there any way to convert PDF to png only the time that I need ?
Thank you!
First, transform your PDF into an image with Imagick, then use your library to decode the QRcode from it:
include_once('lib/QrReader.php');
//the PDF file
$pdf = 'mypdf.pdf';
//retrieve the first page of the PDF as image
$image = new imagick($pdf.'[0]');
//pass it to the QRreader library
$qrcode = new QrReader($image, QrReader::SOURCE_TYPE_RESOURCE);
$text = $qrcode->text(); //return decoded text from QR Code
print $text;
//clear the resources used by Imagick
$image->clear();
You would want to convert the PDF to a supported image type, OR find a QR code reading library that supports PDFs. IMO the first option is easier. A quick search leads me to http://www.phpgang.com/how-to-convert-pdf-to-jpeg-in-php_498.html for a PDF -> img converter.
Presumably the QR code is embedded in the PDF as an image. You could use a command-line tool such as pdfimages to extract the image first, then run your QRReader library on the extracted image. You might need a bit of trial and error to establish which image is the QR code if there is more than one image in the PDF.
See extract images from PDF with PHP for more detail.

How to get the output of a php file into a variable using mPDF

I'm using mPDF library to provide pdf files.
Assume that I have a file named facture.php that contains :
an image in the header
a table in the body
an image in the footer of the file
And another file named convert_HTML2PDF.php that will provide the pdf output of the first file.
I would like to get the output of the facture file that is interpreted into a variable and output it as a pdf file without visualising in the browser.
How can I do that and use the output inside the convert_HTML2PDF.php?
$pdf = $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN)
will output the PDF into a string - no need to create temp file.
Use file_get_contents() to get html content of your facture.php :
$output = file_get_contents('http://www.example.com/facture.php');
$mpdf->Output('filename.pdf', 'F');
will send the output to a file on your server instead of a browser.
See:
http://mpdf1.com/manual/index.php?tid=125
For more output variations.

Bloated PDF created by TCPDF

In a web app developed in PHP we are generating Quotations and Invoices (which are very simple and of single page) using TCPDF lib.
The lib is working just great but it seems to generate very large PDF files. For example in our case it is generating PDF files as large as 4 MB (+/- a few KB).
How to reduce this bloating of PDF files generated by TCPDF?
Here is code snippet that I am using
ob_start();
include('quote_view_bag_pdf.php'); //This file is valid HTML file with PHP code to insert data from DB
$quote = ob_get_contents(); //Capture the content of 'quote_view_bag_pdf.php' file and store in variable
ob_end_clean();
//Code to generate PDF file for this Quote
//This line is to fix a few errors in tcpdf
$k_path_url='';
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF();
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// add a page
$pdf->AddPage();
// print html formated text
$pdf->writeHtml($quote, true, 0, true, 0); //Insert Variables contents here.
//Build Out File Name
$pdf_out_file = "pdf/Quote_".$_POST['quote_id']."_.pdf";
//Close and output PDF document
$pdf->Output($pdf_out_file, 'F');
$pdf->Output($pdf_out_file, 'I');
///////////////
enter code here
Hope this code fragment will give some idea?
You need to see what it is putting inside the PDF. Is it embedding lots of images or fonts?
You can examine the contents with lots of PDFtools. If you have Acrobat 9.0, there is a blog article showing how to do this at http://pdf.jpedal.org/java-pdf-blog/bid/10479/Viewing-PDF-objects
Finally I have managed to solve the problem.
The problem was that by mistake I had inserted a link to email id in the web page that was getting rendered to PDF. By just removing this link the size of the generated PDF went down to just 260 kb!
Thanks everyone who tried to help me out in solving this problem.
Current TCPDF version now includes font subsetting by default to dramatically reduce PDF size.
Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.

Categories