I import external pdf file and try tuuse only first page of it but weight of new pdf is as heavy as full pdf , and is only containing first page when external file contain like 200 pages .
Any idea why , or is maybe some sort of clear command to drop rest of pages from memory of new pdf ??
my code is very simple:
$pdfp = new mPDF('utf-8','A4-L');
$pdfp->displayDefaultOrientation = true;
$pdfp->SetImportUse();
$pagecount = $pdfp->SetSourceFile('ORGINAL.PDF');
///// first page only
$import_page = $pdfp->ImportPage(1);
$pdfp->UseTemplate($import_page);
$pdfp->Output('firstpageonly.pdf', 'F');
any idea , please :)
Regards
Hubert
Related
Hi and thanks by advance for you helps.
I'm trying to write on a PDF with FPDF on PHP.
I'm actually working on WordPress.
If I'm using this code on my first website, it's working well:
if (isset($_GET["obtenir-mon-analyse"])){
$pdfFile = getcwd() . '/wp-content/themes/childtheme/ressources/PDF_analyse_template.pdf';
require_once('library/fpdf/fpdf.php');
require_once('library/fdpi/src/autoload.php');
// initiate FPDI
$pdf = new setasign\Fpdi\Fpdi();
// add a page
$pdf->AddPage("L");
// set the source file
$pdf->setSourceFile($pdfFile);
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 0,0 );
$pdf->Output('I');
}
But, with the same code on another website, the PDF generated by the output function is empty (0kb).
Also, the template is working because FDPF is well detecting the available page number.
PS:
The 2 website are hosting on the same host.
I have not any error.
I really don't know where is the problem.
Thanks a lot.
I think nobody will see this answer but:
On wordpress
With the plugin WP-optimize
If you use the mimify option on HTML, you will not be able to use FPDF
I need to show a new page after the creation of a pdf whit fpdf. My idea is create the page before pdf, cache it in browser, create pdf and than flush the cache and show page but I don't know how to do.
<?php $txt = "hallo";
//My page to be cached
header("..........");
// my pdf
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Cell(40,10,"$txt");
$pdf->Output();
// after pdf I want to show page in the cache
?>
Many thanks
$pdf->Output("Filename.pdf","D");
The second parameter D in double quotes. This will download the file into the user's computer instead of saving it in your server.
I think this is the better way for creating the pdf. :)
http://www.fpdf.org
I have many PDFs that are generated and uploaded to my server.
The problem is they contain the same page three times (3 pages in total with the same content).
My goal is to edit the PDF with PHP so that it contains only one page.
Is there any library that allows me to simply load a PDF and keep only the first page?
Thank you!
Using FPDI, you can create a function to extract the first page of a PDF file:
function first_page ($path) {
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile($path);
$pdf->useTemplate($pdf->importPage(1));
return $pdf;
}
Then output the extracted PDF as you would do with FPDF:
// Extract first page from /path/to/my.pdf
// and output it to browser with filename "MyPDF".
first_page('/path/to/my.pdf')->Output('MyPDF', 'I');
FPDF (http://www.fpdf.org/) or MDPF (http://www.mpdf1.com/mpdf/index.php) are great libraries for work with PDF files. I have experiences only with creating PDF; but I assume that one of those libraries can solve your problem.
Edit: Here is some example with FPDF
https://gist.github.com/maccath/3981205
I have a HTML document which I'm converting to PDF using TCPDF. The HTML document contains several html tables. This works fine. But, I also need to add the number of pages in the PDF doc to the first page. Is there any solution to build the PDF file and then add a page before current first page ?
I don't know if it works with HTML, but I found a solution that worked for me with movePage.
You create the page, get its position and move it to the first position :
$pdf->AddPage();
$pdf->Cell(0, 10, 'My content');
// Get the current page number
$pageNo = $pdf->PageNo();
// First page of the document
$toPage = 1;
$pdf->movePage($pageNo, $toPage);
Official example here : https://tcpdf.org/examples/example_044/
You should be able to calculate how many pages there will be based on the size of your tables. I would read in all the html data, measure it, and then start to make the PDF.
The idea I found is to clone the TCPDF instance and get the returned number of pages:
$tmp_pdf = clone $pdf;
$tmp_pdf->AddPage();
$tmp_pdf->writeHTML($report, true, false, true, false, '');
$num_pages = $tmp_pdf->getNumPages();
unset($tmp_pdf);
$report = preg_replace('#\{num_pages\}#is', $num_pages, $report);
I'm trying to merge 2 PDF, one on my server (not dynamically generated) and one generated just before the merge and not saved anywhere on the server (I just want my client to download it). So I only have the pdf's content. Both PDF have the same format (A4).
The merged file will have 2 pages and won't be saved on server as well.
Since, I'm using Zend Framework, I would prefer a solution with it (can't find one online...) else any advice ?
(common solution found online but doesn't work)
Edit : because people are lazy to click. The code is in the link anyway since it's wrong and doesn't work.
I try the script below, but I get the
error:
Uncaught exception
'Zend_Pdf_Exception' with message
'Page is attached to one documen, but
rendered in context of another
Alright, with the guide from #Gordon 's comment in my question, I got a solution.
You must have at least Zend Framework 1.11 (I was in 1.9, first error) (found thanks to the 3rd comment to this question)
You must clone page from the PDF you want to merge, else, your application will print an error (self explanatory one) (found thanks to this slideshare which is very interesting for Zend_Pdf)
The static PDF must be a PDF <= 1.4 (mine was 1.6). Zend_Pdf can't parse PDF which version is > 1.4
I used this application to convert the static files I had in version 1.6 to 1.4.
Here's the rough code I have and work (I know it's not optimised, I'll do it later; but still, it can be useful)
$pdf2show = new Zend_Pdf(); // Initializing the merged PDF
$pdf1 = Zend_Pdf::parse($pdfContent, 1); // $pdfContent is the generated one, got the content...
$template = clone $pdf1->pages[0]; // cloning the page (a must do)
$page1 = new Zend_Pdf_Page($template); // Creating the first page of the merged PDF with the previous content
$pdf2show->pages[] = $page1; // Adding this page to the final PDF
$pdf2 = Zend_Pdf::load('urlToYourPDF.pdf'); // Loading the statif PDF
$template2 = clone $pdf2->pages[0]; // cloning the page (a must do)
$page2 = new Zend_Pdf_Page($template2); // Creating the second page of the merged PDF with the previous content
$pdf2show->pages[] = $page2; // Adding this page to the final PDF
sendToWebBrowser('title', $pdf2show->render());
sendToWebBrowser is a function sending the PDF content to browser with the title as... title.
$pdf2show->render() produces the merged PDF content as a string.
$extractor = new Zend_Pdf_Resource_Extractor();
$clone_page_to_use_in_any_pdf = $extractor->clonePage($original_pdf->pages[0]);
This is how I did it.
Hope this helps everyone.
TJ
$extractor = new Zend_Pdf_Resource_Extractor();
$page1 = $extractor->clonePage($pdf->pages[$templatePageIndex1]);
$page2 = $extractor->clonePage($pdf->pages[$templatePageIndex2]);
$page1->drawText('Some text...', $x, $y);
$page2->drawText('Another text...', $x, $y);
$pdf = new Zend_Pdf();
$pdf->pages[] = $page1;
$pdf->pages[] = $page2;
Right from the horses mouth.
http://framework.zend.com/manual/en/zend.pdf.pages.html#zend.pdf.pages.cloning
My experience in merging pdfs :
Zend_Pdf is slow and not efficient for large compilations,
pdftk loose document's bookmarks and crashed if document's size is larger than document merged,
pdflib is really fast and preserve bookmarks, is efficient for large compilations.
Have you tried merging them using the PDF toolkit?
Merging multiple PDFs with it can be achieved using :
pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf
123.pdf will be the resulting PDF. You can temporarily store the resulting PDF on the server, send it to the browser and remove it when it's no longer needed.