Php pdf split to pages - php

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.

Related

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

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.

using PHP to fill in a PDF

I need to fill in a PDF, in the fly, using PHP - have no idea where to start.
I'm currently doing quizzes on line using PHP - once a series of quizzes are passed the client wants to let the user download a 'certificate of completion'
The PDF of the certificate has blank lines for the users name and the area of study.
My thought is - add 2 form elements to the PDF, and have PHP pill them in when I pull of the certificate.
BUT HOW?
Is there a different, better way?
Things I need to be 'cautious' of - installing third party stuff is not reliable, UNLESS I can just drop a lib in the the site root. I can't guarantee the hosting provider will let me change a PHP config.
Any help is appreciated - the more specific, the better.
Sorry I have no code at the moment - other than how I'm currently displaying the PDF -
// show cert
echo '<iframe src="1_cert.pdf" width="1000" height="700">';
Thanks.
OK - using TCPDF - as suggested - I've installed, and example pages work...
I've placed a file in the example folder.... I've included a call to import...
require_once('../tcpdf_import.php');
// create new PDF document
$pdf = new TCPDF_IMPORT('1_cert.pdf');
...other boiler plate copied form other examples...
$pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');
// set font
$pdf->SetFont('times', 'B', 20);
$pdf->setPage(1, true);
$pdf->SetY(50);
$pdf->Cell(0, 0, 'test text', 1, 1, 'C');
$pdf->lastPage();
The error I'm getting "TCPDF ERROR: Wrong page number on setPage() function: 1"
Ok, if you're not showing code, then neither will I. ;)
I would recommend doing it with TCPDF.
Import the PDF with TCPDF_Import
In a PDF document, you can navigate to any x/y position, (NB: 0/0 is bottom left). Therefore, simply set your “cursor” to the position to each field, and insert a text with either the Cell or the writeHTMLCell method.
Save the PDF document.
Display it to the user.
Voilà.
By the way, both FPDI and TCPDF are common PHP libraries, so you can just put them somewhere in your base folder, no additional tools should be required on a common web server.

Export a html page to pdf with everything that's written on it, after submit button

I need to export html page to pdf file with everything that's written in it, after I press submit button. It will open new page, with info, and I need for script to automatically make .pdf file (already uploaded to webserver), and get the link from file. Could you give me some easy example (if available, without any plugins, or other features that I must download, I would prefer clean PHP).
Just try this
HTML to PDF with PHP
Using open-source HTML2FPDF project
This solution uses HTML2PDF project (sourceforge.net/projects/html2fpdf/). It simply gets a HTML text and generates a PDF file. This project is based upon FPDF script (www.fpdf.org), which is pure PHP, not using the PDFlib or other third party library. Download [HTML2PDF][1], add it to your projects and you can start coding.
Code example
require("html2fpdf.php");
$htmlFile = "your link";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');

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