I am creating a PDF for our clients website. The PDF is paginated for an A4 print. At the end I need to insert several existing PDFs.
Using the FPDI library works fine except when the inserted PDF is wider than the the A4 width, it doesn't rescale.
In the documentation I've found 2 examples of how to do it, none seems to deliver:
First example
$varPageId = $objPDF->ImportPage($intPageNumber);
$varTemplateSize = $objPDF->getTemplatesize($varPageId);
$objPDF->AddPage(
$varTemplateSize['orientation']
, $varTemplateSize
);
$objPDF->useImportedPage($varPageId);
Second example
$varPageId = $objPDF->ImportPage($intPageNumber);
$objPDF->AddPage();
$objPDF->useTemplate(
$varPageId
, ['adjustpageSize' => true]
);
Would anyone know how I can make sure the inserted PDF gets rescaled and all the content is displayed in the new denerated PDF?
Thanks in advance!
Works as expected. Here is a real example using following following code:
<?php
require_once 'vendor/autoload.php';
$pdf = new \setasign\Fpdi\TcpdfFpdi();
$pageCount = $pdf->setSourceFile('p.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$pageId = $pdf->importPage($pageNo);
$s = $pdf->getTemplatesize($pageId);
$pdf->AddPage($s['orientation'], $s);
$pdf->useImportedPage($pageId);
}
$pdf->Output();
Related
I managed quite easily to add an image to a pdf file with fpdi class with the script below in Laravel environment.
$pdf = new \setasign\Fpdi\Fpdi();
// set PDF file & get page count
$pageCount = $pdf->setSourceFile("MultiplePage.pdf");
// loop pages
for ($p = 1; $p <= $pageCount; $p++) {
// import page p
$tppl = $pdf->importPage($p);
// add page p
$pdf->AddPage();
// use the imported page and adjust the page size
$pdf->useTemplate($tppl, ['adjustPageSize' => true]);
// insert image at position x,y,w,h (in mm)
$pdf->Image("Logo.png",170,5,36,22);
}
// save new pdf
$pdf->Output("NewPDF.pdf", "F");
It works 100% but I struggled to find detailed info how to use each of the command and which parameter units are used (i.e. for $pdf->Image).
Can someone point to that doc please?
My problem is - A user can upload a PDF-File and now I want to add other (between 1 and n) single page PDF's after every Page of the user uploaded PDF.
For example a user uploads a PDF with 4 Pages and I have got 3 single page PDF's on my Webserver that I want to merge with the user PDF. The resulting PDF should then look like this:
Page 1: Page #1 from User uploaded PDF
Page 2: First Single-Page-PDF from Webserver
Page 3: Page #2 from User uploaded PDF
Page 4: Second Single-Page-PDF from Webserver
Page 5: Page #3 from User uploaded PDF
Page 6: Third Single-Page-PDF from Webserver
Page 7: Page #4 from User uploaded PDF
Page 8: First Single-Page-PDF from Webserver
So far I've tried it with PDFMerger
$pdf = new PDFMerger;
$pdf->addPDF('samplepdfs/userpdf.pdf', 'all')
->addPDF('samplepdfs/one.pdf', 'all')
->addPDF('samplepdfs/two.pdf', 'all')
->addPDF('samplepdfs/three.pdf', 'all')
->merge('file', 'xxx/result.pdf');
but this only adds the other PDF's at the end..
Is there a solution in PHP where I can specify the number of the page where the other PDF's shall be inserted?
You should avoid using the PDFMerger class. It is almost 6 (!!!) years old!
You can do the same with FPDI without much effort. Anyhow you should insert the pages in the order you want them to appear. So I guess it's more a logic problem. From what you'd written in your question I assume that you want to import a whole document while spreading another document with 3 pages between the imported pages. So a simple POC could look like this:
<?php
require_once("libs/fpdf/fpdf.php");
require_once("libs/fpdi/fpdi.php");
$pdf = new FPDI();
// let's prepare the static "filler pages"
$staticIds = array();
$pageCount = $pdf->setSourceFile('pdf/on/the/webserver.pdf');
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$staticIds[$pageNumber] = $pdf->importPage($pageNumber);
}
// get the page count of the uploaded file
$pageCount = $pdf->setSourceFile('the/uploaded.pdf');
// let's track the page number for the filler page
$fillerPageCount = 1;
// import the uploaded document page by page
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$tplId = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($tplId, null, null, 0, 0, true);
// add the current filler page
$pdf->AddPage();
$pdf->useTemplate($staticIds[$fillerPageCount], null, null, 0, 0, true);
// update the filler page number or reset it
$fillerPageCount++;
if ($fillerPageCount > count($staticIds)) {
$fillerPageCount = 1;
}
}
// done
$pdf->Output('xxx/result.pdf', 'F');
I try to use the right axis of TeeChart for PHP. I'm aware that we need to link a valid serie to both vertical axis. In fact, I have tried a simple test with the custom axis demo on the Steema site. I cut and pasted the demo and try to export it to javascript instead of rendering it.
I used this code to export to javascript :
echo $tChart1->getChart()->getExport()->getImage()->getJavaScript()->Render()->toString();
Here is a snapshot of the 2 renders side-by-side (sorry to put it in a link, this forum don't allow me to post pictures yet...)
Is there a way to get the right axis to show with the export?
EDIT:
Here is the code to test on your side :
<?php
//Includes
include "../../../sources/TChart.php";
$chart1 = new TChart(600,450);
$chart1->getChart()->getHeader()->setText("Custom Axes Demo");
$chart1->getAspect()->setView3D(false);
$line1 = new Line($chart1->getChart());
$line2 = new Line($chart1->getChart());
$line1->setColor(Color::RED());
$line2->setColor(Color::GREEN());
$chart1->addSeries($line1);
$chart1->addSeries($line2);
// Speed optimization
$chart1->getChart()->setAutoRepaint(false);
for($t = 0; $t <= 10; ++$t) {
$line1->addXY($t, (10 + $t), Color::RED());
if($t > 1) {
$line2->addXY($t, $t, Color::GREEN());
}
}
$chart1->getAxes()->getLeft()->setStartPosition(0);
$chart1->getAxes()->getLeft()->setEndPosition(50);
$chart1->getAxes()->getLeft()->getAxisPen()->color = Color::RED();
$chart1->getAxes()->getLeft()->getTitle()->getFont()->setColor(Color::RED());
$chart1->getAxes()->getLeft()->getTitle()->getFont()->setBold(true);
$chart1->getAxes()->getLeft()->getTitle()->setText("1st Left Axis");
$chart1->getAxes()->getTop()->getLabels()->setAngle(45);
$chart1->getAxes()->getTop()->getTitle()->getFont()->setColor(Color::YELLOW());
$chart1->getAxes()->getTop()->getTitle()->getFont()->setBold(true);
$chart1->getAxes()->getBottom()->getLabels()->setAngle(0);
$chart1->getAxes()->getRight()->getLabels()->setAngle(45);
$chart1->getAxes()->getBottom()->getTitle()->getFont()->setColor(new Color(255,25,25));
$chart1->getAxes()->getBottom()->getTitle()->getFont()->setBold(true);
$chart1->getAxes()->getRight()->getTitle()->getFont()->setColor(Color::BLUE());
$chart1->getAxes()->getRight()->getTitle()->getFont()->setBold(true);
$chart1->getAxes()->getRight()->getTitle()->setText("OtherSide Axis");
$chart1->getAxes()->getRight()->getLabels()->getFont()->setColor(Color::BLUE());
$chart1->getAxes()->getRight()->getAxisPen()->setColor(Color::BLUE());
$chart1->getAxes()->getTop()->getTitle()->setText("Top Axis");
$chart1->getAxes()->getBottom()->getTitle()->setText("Bottom Axis");
$line1->setHorizontalAxis(HorizontalAxis::$BOTH);
$line1->setVerticalAxis(VerticalAxis::$BOTH);
$axis1 = new Axis(false, false, $chart1->getChart());
$chart1->getAxes()->getCustom()->add($axis1);
$line2->setCustomVertAxis($axis1);
$axis1->setStartPosition(50);
$axis1->setEndPosition(100);
$axis1->getTitle()->getFont()->setColor(Color::GREEN());
$axis1->getTitle()->getFont()->setBold(true);
$axis1->getTitle()->setText("Extra Axis");
$axis1->getTitle()->setAngle(90);
$axis1->setRelativePosition(20);
$axis1->getAxisPen()->setColor(Color::GREEN());
$axis1->getGrid()->setVisible(false);
echo $tChart1->getChart()->getExport()->getImage()->getJavaScript()->Render()->toString();?>
I've modified the end of your test page to show both the HTML5 and the PHP charts at the same page:
echo $chart1->getChart()->getExport()->getImage()->getJavaScript()->Render()->toString();
$chart1->render("chart1.png");
$rand=rand();
print '<img src="chart1.png?rand='.$rand.'">';
Then, I've modified TeeChart PHP sources to also export the custom axes and the assign.
It now looks like this:
Please, send a mail to "info#steema.com" and we'll send you the modified unit (JavaScriptExport.php).
I'm trying to use zend pdf to load multiple pdfs from file (each a 1 page pdf) then draw text, images etc. onto each one. Ideally the drawn text and images would be added within a function. However when I try to do some basic draw it comes back with an error in the pdf. The code works perfectly fine until I try draw text onto the page.
require_once 'zendframework/library/Zend/Loader/Autoloader.php';
include 'form-structures.php'; //
$loader = Zend_Loader_Autoloader::getInstance();
$pdfMerged = new Zend_Pdf();
//load pdf
$loadedpdf = Zend_Pdf::load("form-url.pdf");
//clone pdf
$page1 = clone $loadedpdf->pages[0];
// add text etc.
$page1->drawText('Some text...', 400, 500);
drawmore($page1);
//merge pdf
$pdfMerged->pages[] = $page1;
// do again
$loadedpdf = Zend_Pdf::load("form-url.pdf");
$page = clone $loadedpdf->pages[0];
$pdfMerged->pages[] = $page;
echo $pdfMerged->render();
The form-structure.php file is below with a function to draw something extra on the page:
function drawmore($page)
{
$page->drawText("Height is: 600px", 300,800);
return $page;
}
Resolved.
Zend PDF can't draw text on the screen unless you already specified the font.
I just added:
$page1->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
and it fixed the error.
I'm trying to do some PDF generation in PHP. I found mPDF which generally has worked quite well. One problem I'm having though is that I need to be able to set separate headers for even/odd pages for some sections. I don't think the function is working right. Here's my code:
// load mPDF
// --------------------------------------------------------------------------
include('mpdf.php');
$mpdf = new mPDF();
// generate a lot of content so it spans multiple pages
// --------------------------------------------------------------------------
$i = 0;
$out = '';
while ($i < 300)
{
$i++;
$out .= '<p>Lorem Ipsum</p>';
}
// set html header for odd pages, write html and output
// --------------------------------------------------------------------------
$mpdf->SetHTMLHeader('ODD {PAGENO}', 'O');
$mpdf->WriteHTML($out);
$mpdf->Output();
When I run this code it puts "ODD Page 3" or whatever page it's on... on every page. Not just the odd pages. It seems to not consider naturally broken pages as new pages for the purposes of even or odd, only manually broken ones. If I run the AddPage() function it will consider the next group an even page. But that could be 50 auto page breaks later.
Any suggestions on how to get it to set different headers for actual even/odd pages, not just after manual page breaks?
your forget to write
$mpdf->mirrorMargins = 1;
here is your working example
<?php
include("../mpdf.php");
$mpdf=new mPDF('utf-8','A4');
$mpdf->debug = true;
$mpdf->mirrorMargins = 1 ;// Use different Odd/Even headers and footers and mirror margins
// generate a lot of content so it spans multiple pages
// --------------------------------------------------------------------------
$i = 0;
$out = '';
while ($i < 300)
{
$i++;
$out .= '<p>Lorem Ipsum</p>';
}
// set html header for odd pages, write html and output
// --------------------------------------------------------------------------
$mpdf->SetHTMLHeader('{PAGENO}/{nb}', 'O',true);
/* Note: SetHTMLHeader() and SetHTMLFooter() without a side(2nd argument)
- sets ODD page header/footer only as default..so you can also write just
$mpdf->SetHTMLHeader('{PAGENO}/{nb}'); */
$mpdf->WriteHTML($out);
$mpdf->Output();
exit;
?>
Reference:
setHTMLHeader