Invalid characters when call output method with custom fonts in dompdf php - 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

Related

Php pdf split to pages

I have pdf files. Need import them to system and split by pages. Seperate page = separate file.
I tried use FPDF and FPDI:
$pdf = new FPDI(); //FPDI extends TCPDF
$pdf->AddPage();
$pages = $pdf->setSourceFile($sLeadDirPath . $sImageName);
//for first page...
$page = $pdf->ImportPage(1);
$pdf->useTemplate($page, 0, 0);
$pdf->Output('newTest.pdf', 'F');
Some files work good, but for some files i got error:
Fatal error: Uncaught exception
'setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException' with
message 'This PDF document is encrypted and cannot be processed with
FPDI.'
As I understood file is encrypted (but i can open this without any problems).
How can I solve this?
Or maybe there are other ways for split pdf in PHP without FPDI?
Thanks!
A PDF can be encrypted even though you are not required to enter a password to see the contents of the document. This can be the case when the PDF file does not have an 'document open password' (user password) but has a 'permissions password' (master password). When this is the case, you will be able to open the PDF without entering a password, but you will not be able to edit the PDF (manually or through FPDI) without entering that password. You will have to decrypt the PDF, however, this is not implemented in FPDI:
FPDI does not support importing of encrytped [sic] PDF documents. After all
this makes no sense, because the resulting document is a completely
new document which may be re-encrypted or not.
(...)
If you need to modify an existing document, that is already encrypted
you may checkout any SetaPDF product.

Pimcore how to create PDF and send via email

Anybody can give us a hint how to treat this best, if this can be achieved with „standards“ of pimcore?
Render a PDF with dynamic content and merge it with a PDF which has legal information.
Then send final pdf via email.
The PDF should be able to designed with HTML
You can use the Web2Print Documents to create and render the first PDF with dynamic content. To modify the PDF afterwards, you can hook into the PDF generation process (Pimcore 4 example):
\Pimcore::getEventManager()->attach("document.print.postPdfGeneration", function (\Zend_EventManager_Event $e) {
$document = $e->getTarget();
$pdf = $e->getParam("pdf");
Once you got that PDF, you can merge it with other PDFs like described here: Merge PDF files with PHP
Sending it via email should be easy at this point.

Guzzle 6 - Use a .pdf generate by a .php page

I a controller which use an external page in charge of
generating a pdf with the help of the FPDF class.
To see if the generation is working on the server, I told to FPDF to save the file, then ask the browser to display the pdf.
Guzzle do his job, he call the page, the pdf is save and the content of the page download.
The pdf save is not corrupted and work perfectly.
The response is a pdf window showing a 'failed to load PDF document' error.
I'm using laravel, there's my controller :
$response = $client->request('POST','http://.../x.php', [
'form_params' => [...]
]);
return $response;
Laravel doesn't use PSR-7 for Request/Response, so you have to convert Guzzle's response to Laravel's one.

Moodle download a created PDF

I create a PDF in Moodle using the following code
$pdf = new pdf;
$pdf->AddPage();
$pdf->Write(1, "Test");
$pdf->Output();
How would I make this download in the browser instead of opening in browser?
// Force the browser to download the output
$pdf->Output('filename.pdf','D');
Moodle wraps the TCPDF library for PDF generation (the wrapper mostly just handles the locations for temporary files and accessing embedded images which are in the Moodle Files API).
You can find documentation about the TCPDF Output() function online at http://www.tcpdf.org/doc/code/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
The important param is the second one, calling $pdf->Output('filename.pdf', 'D') will cause it to download.

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