I'm attempting to Upload a PDF as a template for the cover page, then add html for the middle pages, then add a PDF as a template again for the final page.
Here is my code right now:
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
$mpdf->SetDisplayMode('fullpage');
$mpdf->enableImports = true;
$mpdf->debug = true;
$mpdf->SetImportUse();
//Set Cover Page Template
$pagecount = $mpdf->SetSourceFile('site/themes/raven/pdf/cover-page.pdf');
$tplId = $mpdf->ImportPage($pagecount);
$actualsize = $mpdf->SetPageTemplate($tplId);
// Add First page
$mpdf->AddPage();
//Write Content on Inside Pages
$html= (string) get_content("/print-menu");
$mpdf->AddPage();
$mpdf->WriteHTML($html);
//Set Last Page Template
$pagecount2 = $mpdf->SetSourceFile('site/themes/raven/pdf/last-page.pdf');
$tplId2 = $mpdf->ImportPage($pagecount2);
$actualsize2 = $mpdf->SetPageTemplate($tplId2);
//Add Last Page
$mpdf->AddPage();
$mpdf->Output();
I've tried UseTemplate() and UsePageTemplate() and tried changing the order of operations. It works on the first page, however the last page appears as a blank page.
I had the same problem.
In my case, the solution was to set the background-color to transparent, because there was set a background-color in the content. So the background will be rendered over the last page
Related
I am trying to create an invoice document and display the items in pdf.
The problem I have is that the first page will use a pdf template(Using FPDI) that has the Company logo, company information and the list of items bought.
If the list of items will require more than one page, the next pages will need to use second page of pdf template that only shows the company logo as a header and the continuation of the items bought. Below is my sample code. I also tried to extend the fpdi based on this SO post but I cant make it work.
$pdf = new FPDI();
ob_start();
echo $this->view->print_invoice($productItems, $customerInfo);
$content = ob_get_contents();
ob_clean();
// add a page
$pdf->addPage();
$pdf->writeHTML($content);
$pageCount = $pdf->setSourceFile(ROOT_DIR . "public/pdf-templates/group.pdf");
$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->useTemplate($tplIdx, 10, 10, 190, 280);
$pdf->Output('outfile.pdf','I');
Thanks in advance for your help!
Just implement the logic you describe in a header() method of an extending class:
class PDF extends FPDI
{
public function Header()
{
if ($this->PageNo() > 1) {
$tplIdx = $this->importPage(2);
} else {
$tplIdx = $this->importPage(1);
}
$this->useTemplate($tplIdx);
}
}
ob_start();
echo $this->view->print_invoice($productItems, $customerInfo);
$content = ob_get_contents();
ob_clean();
$pdf = new PDF();
$pdf->setSourceFile(ROOT_DIR . "public/pdf-templates/group.pdf");
$pdf->addPage();
$pdf->writeHTML($content);
$pdf->Output('outfile.pdf','I');
I am converting an html file to PDF by using mPdf, but the returned PDF file includes blank pages. I want to delete those blank pages.
Had the same problem and realized that my CSS was forcing these page breaks. Make sure you don't have this in your CSS:
page-break-after: always;
It may be for many reasons:
1) Make sure the elements doesn't have unnecessary margins nor Paddings
2) Configure correctly the page properties (specially margins) :
$page_orientation = 0;
$page_size = 'Letter';
$page_margins = array('LEFT(int)','RIGHT(int)','UP(int)','BOTTOM(int)');
$pdf_output = 'I';
$css_files = array(
'path/file.css',
'path/file_2.css',
);
$orientationPage = array('','-L');
/* ===== [ MPDF ] =====*/
$mpdf=new mPDF('utf-8', $page_size.$orientationPage[$page_orientation],'','',$page_margins[0],$page_margins[1],$page_margins[2],$page_margins[3]);
// Loading CSS
for($i = 0; $i < count($css_files); $i++){
$stylesheet = file_get_contents('../../'.$css_files[$i]);
$mpdf->WriteHTML($stylesheet,1); // 1 para indicar que es CSS
}
// Set header & Footer (This are variables with html code)
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetTitle($title);
$mpdf->WriteHTML($html); // <-- insert HTML
// Create PDF
$mpdf->Output($titulo.'.pdf',$pdf_output);
3) Make sure you haven't unnecessary "Page Breaks" on the HTML
<pagebreak type="NEXT-ODD" resetpagenum="1" pagenumstyle="i" suppress="off" />
I hope it can help you!
I had the same problem and i fixed it by removing the AddPage property from my code
I changed the following code
// Code with bug
$mpdf = new mPDF('utf-8', array(380,500));
$mpdf->WriteHTML($htmlContent);
$mpdf->AddPage('P'); // It will add extra page - I that i removed this line
$mpdf->Output();
Into this
// code after resolving the bug
$mpdf = new mPDF('utf-8', array(380,500));
$mpdf->WriteHTML($htmlContent);
$mpdf->Output();
Hi I'm using FPDF and FPDI, I'm using FPDI to concatenate several PDFs then using FPDF to fill in the information based on a form that is filled out, I've setup a SetPage method within FPDF to be able to set the page on which I'm working on, I'm able to write on the first file completely fine (first 3 pages). However, when I'm trying to write on the second file (4th and continuing pages), I use the SetXY and Write but nothing is written, I am able to add an image (barcode at the bottom of the page) but no text, any ideas as to what I'm doing wrong?
This is the code that I've got to concatenate the files:
<?php
session_start();
require_once('lib/pdf/fpdf.php');
require_once('lib/pdi/fpdi.php');
require_once('lib/barcode/class/BCGFontFile.php');
require_once('lib/barcode/class/BCGColor.php');
require_once('lib/barcode/class/BCGDrawing.php');
require_once('lib/barcode/class/BCGcode39extended.barcode.php');
$contractType = $_SESSION['addition'];
require_once('barcode.php');
if(isset($contractType))
{
$files = array('lib/blank/NDA.pdf');
if($contractType = 'artist')
{
array_push ($files,
'lib/blank/Distro.pdf',
'lib/blank/Management-Trial.pdf'
);
} else {
echo "Whoops! Something must've happened when you were filling out your contracts! Please try filling them out again. Sorry!";
}
}
$pdf = new FPDI();
foreach ($files AS $file) {
$pageCount = $pdf->setSourceFile($file);
for($n = 1; $n <= $pageCount; $n++) {
$tmpIdx = $pdf->importPage($n);
$size = $pdf->getTemplateSize($tmpIdx);
if($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
$pdf->useTemplate($tmpIdx);
}
}
//NDA FILLER
include('lib/filler/NDA.php');
//Distro Contract Filler
include('lib/filler/Distro.php');
//session_unset();
$pdf->Output();
?>
This is the code for filling out the first PDF (which works completely fine):
NDA.php
<?php
//ID No.
$idcoded = 'idbars/'.$_SESSION['name'].'.png';
/*
for($p = 2; $p <= $pages; $p++)
{
$pdf->Image($idcoded,0,350);
$pdf->setPage($p);
}
*/
$pdf->SetPage(1);
$pdf->Image($idcoded,0,350);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
//NDA DATE
$pdf->SetXY(51, 109.5);
$pdf->Write(0, date(d));
$pdf->SetXY(72, 109.5);
$pdf->Write(0, date(F));
//Legal Name
$pdf->SetXY(72, 114.5);
$pdf->Write(0, $_SESSION['name']);
//stage Name
$pdf->SetXY(80, 119.5);
$pdf->Write(0, $_SESSION['sname']);
$pdf->setPage(2);
$pdf->Image($idcoded,0,350);
$pdf->setPage(3);
$pdf->Image($idcoded,0,350);
$signature = 'idbars/'.$_SESSION['name'].'_sig.png';
$pdf->Image($signature,20,105,100);
?>
This is what I'm using to try to write on the second PDF, I've tried combining the NDA.php and Distro.php into one file and that makes no difference
Distro.php
<?php
$pdf->SetPage(4);
$pdf->SetXY(10,10);
$pdf->Cell(0, $_SESSION['name']);
$pdf->Write(0, $_SESSION['name']);
$pdf->Image($idcoded,0,350);
?>
The page that this is building works off of this form:
https://secure.gr8label.com/sign/artist/Dev%20Test/
FPDF "caches" the font information that is currently used. As you jump back to another page FPDF "thinks" that the font is already defined/set but in the PDF file itself it isn't. You should set your font and size in your import loop, to ensure that the font is available on all pages (I think it also could work, by defining it only on the first one).
Anyhow you should have seen that jumping between written pages results in problems and you should create a logic which creates the file from top to bottom without using things like "SetPage()" at all.
I am getting problem in anchor tag while export to PDF.Anchor tag is working for static url like http:google.com but it is not working for dynamic url.I am using mpdf module for PDF.
$url = $fullBaseUrl.'/designers/attachment/time/'.$value['filetime'].'/uploadTab/imgattach';
// http://localhost/msme_latest/designers/attachment/time/1394432246/uploadTab/imgattach
$html= ''.$value['filename'].'';
// echo $html; die;
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
When i echo my code $html it is giving my link properly.But when i export this code in PDF it is not giving any kind of link PDF.
Any help will be appriciated.
For solve this problem you have to make just i change in mpdf.php
Actual Code is in mpdf.php on line no.20146
if(isset($vetor[1]) and $vetor[1] != '') //LINK
{
if (strpos($vetor[1],".") === false && strpos($vetor[1],"#") !== 0) //assuming every external link has a dot indicating extension (e.g: .html .txt .zip www.somewhere.com etc.)
{
//Repeated reference to same anchor?
/*
while(array_key_exists($vetor[1],$this->internallink)) $vetor[1]="#".$vetor[1];
$this->internallink[$vetor[1]] = $this->AddLink();
$vetor[1] = $this->internallink[$vetor[1]];
*/
}
$this->HREF = $vetor[1]; // HREF link style set here ******
}
and you have just comment line of code (line number:20151 to 20153)
/*
while(array_key_exists($vetor[1],$this->internallink)) $vetor[1]="#".$vetor[1];
$this->internallink[$vetor[1]] = $this->AddLink();
$vetor[1] = $this->internallink[$vetor[1]];
*/
and your pdf will accept all link include "localhost" and other external links.
use Ip address instead of localhost or use Live server url it is working for me
<?php
$fullBaseUrl = "http://127.0.0.1/meme_latest";
$url = $fullBaseUrl.'/designers/attachment/time/'.$value['filetime'].'/uploadTab/imgattach';
//(http://localhost/msme_latest/designers/attachment/time/1394432246/uploadTab/imgattach)
$html= 'Test link';
//echo $html; die;
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
This looks like the simplest thing but I can't get it to work.
I need to add text to the first page of a multi-page pdf (could be any number of pages)
Using this code on a two page pdf (without the for loop, just using $pdf->importPage(2)) I end up with two pages but the second page is a repeat of page one. The text is written on the first page only which is good but I need all pages included in the output pdf. Here is my code
// Original file with multiple pages
$fullPathToFile = 'full/path/to/file.pdf';
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile;
if (is_null($this->_tplIdx)) {
$this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
function Footer() {}
}
// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);
// add a page
$pdf->AddPage();
// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');
// How to get the number of pages of original pdf???
// $numPages = $pdf->getNumPages(???);
// Carry on adding all remaining pages starting from page 2
for($i=2;$i<=$numPages;$i++) {
// Add another page
$pdf->AddPage();
// Do I need to declare the source file here?
// $pdf->setSourceFile($fullPathToWD);
$pdf->importPage($i);
}
// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');
Links to docs
TCPDF Classes
http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced
FPDI Classes
http://www.setasign.de/support/manuals/fpdi/
FPDF_TPL Classes
http://www.setasign.de/support/manuals/fpdf-tpl/
Solved my problem...
// Original file with multiple pages
$fullPathToFile = 'full/path/to/file.pdf';
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile;
if (is_null($this->_tplIdx)) {
// THIS IS WHERE YOU GET THE NUMBER OF PAGES
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
function Footer() {}
}
// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);
// add a page
$pdf->AddPage();
// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');
// THIS PUTS THE REMAINDER OF THE PAGES IN
if($pdf->numPages>1) {
for($i=2;$i<=$pdf->numPages;$i++) {
$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
}
}
// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');
You get the number of pages by adding the first part of this line
$this->numPages = $this->setSourceFile($fullPathToFile);
And see the second last block of code - the for loop adds the remainder of the pages.
Don't know if this is how it should be done? I read in a few places that it wasn't even possible to achieve this, also the code is not supplied in the docs. However, this works, hope it helps someone.
I struggled with this a little and tried to come up with simplest way to add some text to the last page of a multi-page document. Here is the very simple code that worked for me:
require_once('fpdf/fpdf.php');
require_once('fpdf/fpdi.php');
$pdf = new FPDI();
$fullPathToPDF = '/usr/local/common/my.pdf';
$pageCount = $pdf->setSourceFile($fullPathToPDF);
for ($i = 1; $i <= $pageCount; $i++) {
$pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($i);
}
$pdf->SetFont('Helvetica');
$pdf->SetXY(110, 225);
$pdf->Write(8, 'A complete document imported with FPDI');
$pdf->Output($fullPathToPDF);
Just change the full path to file to a location where you have a multi-page PDF.
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
...
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(true, 10);
foreach($array as $item)
{
$pdf->AddPage(); //add new page for new item
$txt = some_long_long_text;
$pdf->Write(0, $txt, '', 0, 'C', true);
$pdf->endPage(); //do end of page
$pdf->lastPage(); //set cursor at last page, because autopagebreak not do it
}
In example, you have 10 pupils in array, and you need create resume for each. In exam, one resume have 3 pages. So in out u get pdf with 30 pages, with correct text.
SetAutoPageBreak(true, 10), not set cursor at last page, so you need to do it manually with function $pdf->lastPage();
that code wont work, try this:
$pdf = new PDI();
$pdf->AddPage();
$pdf->setSourceFile('zzz.pdf');
$pdf->numPages = $pdf->setSourceFile('zzz.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 20, 200);
if($pdf->numPages>1) {
for($i=2;$i<=$pdf->numPages;$i++) {
$pdf->AddPage();
$tplIdx = $pdf->importPage($i);
$pdf->useTemplate($tplIdx, 10, 20, 200);
}
}