Use different image from two PDF for create one PDF with FPDI/TCPDF - fpdf

I would use two differents pages from two differents PDF for create one PDF with both pages combines.
For that I use FPDF and I have done this :
$finalPDF = new Fpdi();
$secondPDF = new Fpdi();
$personalizationPdfPath = "Path_of_my_first_PDF";
$templatePath = "Path_of_my_second_PDF";
$finalPDF->setSourceFile($templatePath);
$secondPDF->setSourceFile($personalizationPdfPath);
// Import the first page
$page1 = $pdfFinal->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);
// Import the second page of second PDF
$page2 = $secondPDF->importPage(2, PdfReader\PageBoundaries::MEDIA_BOX);
// Get the size
$dimension = $finalPDF->getTemplateSize($template_page);
// Add the page
$finalPDF->AddPage($dimension["orientation"], array($dimension["width"], $dimension["height"]));
// Apply the page1 on the finalPDF
$finalPDF->useTemplate($page1, 0, 0, $dimension["width"], $dimension["height"]);
// Apply the page2 on the finalPDF
$finalPDF->useTemplate($page2, 20, 28, $dimension["width"]*0.75, $dimension["height"]*0.75); //error
But when I run it, I have Template does not exist error. If I put $page1 instead of $page2 it work, both pages are combine. The first 100% size and second 75% size.
I don't know why the $page2 not working. I have used dd(dump die) to see the difference between both $pages, nothing revelant.
So I use an alternative, transform the $page2 into a picture and use AddImage method :
$imageFromPDF = "Path_of_my_image.jpg";
$finalPdf->Image($imageFromPDF, 35, 35, $dimension["width"]*0.70, $dimension["height"]*0.70, "JPG");
$pdfFinal->Output("F", "nameOfPdf");
It works good, but the quality is bad. I have read this subject but the quality still trash.
On both way, anyone has a good solution ?
Thanks

Just do it step by step. There's no need for 2 FPDI instances.
$pdf = new Fpdi();
// set the first document as the source
$pdf->setSourceFile($templatePath);
// then import the first page of it
$page1 = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);
// now set the second document as the source
$pdf->setSourceFile($personalizationPdfPath);
// and import the second page of it:
$page2 = $pdf->importPage(2, PdfReader\PageBoundaries::MEDIA_BOX);
// to get the size of an imported page, you need to pass the
// value returned by importPage() and not an undefined variable
// such as $template_page!!
$dimensions = $pdf->getTemplateSize($page1); // or $page2?
// Add a page
$pdf->AddPage($dimension["orientation"], $dimension);
// ...now use the imported pages as you want...
// Apply the page1 on the finalPDF
$pdf->useTemplate($page1, 0, 0, $dimension["width"], $dimension["height"]);
// Apply the page2 on the finalPDF
$pdf->useTemplate($page2, 20, 28, $dimension["width"] * 0.75, $dimension["height"] * 0.75);
The values 20 and 28 looks odd to me but it's what you'd used.

Related

Using FPDF and FPDI together

Im generating a PDF using FPDF which works fine, when certain conditions are met i want to be able to import an existing PDF (sometimes with multiple pages), what im finding though is the FPDI import is overwriting any existing FPDF page generation.
Here is some sample code
if($row['art'] <> '')
{
$datasubject = mysqli_query($link,"SELECT * from documents WHERE subject = 'Art'");
$rowsubject = mysqli_fetch_array($datasubject);
$pdf->AddPage("P","A4");
$pdf->SetY(30);
$pdf->cell(350,20,"Art Page",0,'C',false);
$pdf = new \setasign\Fpdi\Fpdi();
// set the source file
$pageCount = $pdf->setSourceFile("C:\\temp\\sourcedocuments\\Year 7\\art\\KS4 FINE ART KNOWLEDGE ORGANISER higher tier.pdf");
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$tplIdx = $pdf->importPage($pageNo);
// add a page
$pdf->AddPage();
$pdf->useTemplate($tplIdx, null, null, 210, 300, true);
// font and color selection
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(200, 0, 0);
}
}
Before it hits this "IF " statement FPDF creates a couple of static "Cover" pages but these are also overwritten by the imported document.
Moved $pdf = new \setasign\Fpdi\Fpdi(); from out of the "If" statement and replaced
$pdf = new FPDF(); with $pdf = new \setasign\Fpdi\Fpdi();

Codename One - Bar Chart

Following the doc and the example provided by cno I tried making a Bar Chart but despite the tries and the tests I always fall on the same result. I am sure I either missed something or I made a mistake somewhere, maybe there is some parameter I didn't understand well like the scale
Current BarChart Code
public Form createBarChartForm()
{
XYMultipleSeriesRenderer rendererTwo
= new XYMultipleSeriesRenderer(300);
rendererTwo.setBarWidth(300);
// rendererTwo.addYTextLabel(1, "ok");
// rendererTwo.addXTextLabel(5, "Ouuhh");
rendererTwo.setXAxisMin(1, 50);
rendererTwo.setXAxisMax(5, 200);
rendererTwo.setGridColor(ColorUtil.GREEN, 10);
// rendererTwo.setDisplayValues(true);
// rendererTwo.setYAxisMin(5);
// rendererTwo.setYAxisMax(5, 10);
com.codename1.charts.models.XYMultipleSeriesDataset dataset
= new XYMultipleSeriesDataset();
XYSeries xYSeries = new XYSeries("Hi", 50);
XYSeries xYSeries2 = new XYSeries("Hello", 150);
XYSeries xYSeries3 = new XYSeries("Hola", 80);
dataset.addSeries(xYSeries);
dataset.addSeries(xYSeries2);
dataset.addSeries(xYSeries3);
com.codename1.charts.views.BarChart chart = new com.codename1.charts.views.BarChart(
dataset, rendererTwo, BarChart.Type.STACKED);
// Wrap the chart in a Component so we can add it to a form
ChartComponent c = new ChartComponent(chart);
// Create a form and show it.
Form f = new Form("Budget", new com.codename1.ui.layouts.BorderLayout());
f.add(com.codename1.ui.layouts.BorderLayout.CENTER, c);
return f;
}
Result (Always The same when no errors)
Result (Stack Overflow upload would show at the bottom right and not entierly)
I found a chart demo on cno's website and it was more than I asked for.
Link To Charts Demo
It contains demo for all charts and it's well made.
I download the git project and tested out the one I need (extracted some methods) and it works. I would say I was 50% in the way of getting there.
I apologize if this was useless as I didn't really think the website would contain demo and I just fell on the site randomly.

TYPO3: Trying to add link to images

On our site, other admins add images via the "Resources" tab of the main page. These images are displayed as Banners in a Slider on the main page. However, now they want the ability to add links to specific images.
My first thought on this (after receiving some help on making a loop for images to be added to the page) was to perhaps let them be able to add the link to either the "Title" or "Caption" spot I saw there. And later, on the slider "create" function, pull the said data from the image and make <a> wrap around the image before the slider finished building. I've already tested the slider plugin with this functionality, and that would work fine, however, I can't seem to pull anything from the "Title" or "Caption" and add it to the image in any way.
My other thought would be, is there a way to extend the back end to give them an actualy spot to paste links on images so that I may pull that and wrap the image via the typoscript, or can i pull from caption and wrap image in <a> "if" the link is available.
In other words, does typoscript have a type of "if" statement? What I ahve so far, thanks to maholtz is as follows:
#BANNER IMAGES LOOP BEGIN
page.10.marks.topimage = TEXT
page.10.marks.topimage {
# retrieve data
data = levelmedia: -1, "slide"
override.field = media
# we have some filenames in a list, let us split the list
# and create images one by one
# if there are five images selected, the CARRAY "1" will be executed
# five times where current is loaded with only one filename
split {
# the images are separated via ","
token = ,
# you can do funny stuff with options split, f.e. if you want to give first
# and last image a different class... but thats another topic;)
# we just say, render every splitted object via CARRAY "1"
cObjNum = 1
1 {
# just render the single image,
# now there should be one filename in current only
10 = IMAGE
10 {
file.import.wrap = fileadmin/user_upload/|
file.import.current = 1
border = 0
file.height = 670
file.width = 1800
altText = Banner
titleText = Banner
# attempt to add link to image if available
caption.1.typolink.parameter.field = image_link
caption.1.typolink.parameter.listNum.stdWrap.data = register:IMAGE_NUM_CURRENT
}
}
}
wrap = <div id="slides">|</div>
}
#BANNER IMAGES LOOP END
I was thinking perhaps I could do something like:
10 {
file.import.wrap = fileadmin/user_upload/|
file.import.current = 1
border = 0
file.height = 670
file.width = 1800
altText = Banner
titleText = Banner
# attempt to add link to image if available
caption.1.typolink.parameter.field = ???
caption.1.typolink.parameter.listNum.stdWrap.data = register:IMAGE_NUM_CURRENT
}
But as you can see, I'm stumped on how that might even work right. Can anyone help point me the right way.
As before mentioned, perhaps I could do ONE of two things:
Pull link from "Title" or "Caption" and add it to the IMAGE Date on output so that I can use that client side to wrap the image in appropriate a tag, |OR|
Pull link from there and use typoscript to wrap the image in a tags
When accessing the ressources via levelmedia = slide you're not directly accessing the FAL table. Therefore you have to load it again to access the fields you want. We solved exactly the problem you have with the following code. Insert it inside your 1 after 10 = IMAGE.
typolink{
parameter{
cObject = RECORDS
cObject{
source.current = 1
tables = sys_file_reference
conf.sys_file_reference = TEXT
conf.sys_file_reference.field = #title or description
}
}
}

Zend_Pdf and templated

I have problems creating a template for my pdf documents in Zend_Pdf. I've followed the guide provided at http://framework.zend.com/manual/en/zend.pdf.pages.html but it doesn't seem to work since no footer text or logo are visible at any page except the first.
This is a sample of my code: (Note; the MTP constant is just the recalculate from mm to points)
//Create pdf object
$pdf = new Zend_Pdf();
//Create the first page
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
/*HEADER*/
//insert logo at the top
$logo = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH . '/../document_root/images/logotype.png');
$page->drawImage($logo, 22*MTP,274*MTP, 62*MTP, 289*MTP);
/*FOOTER*/
$footerStyle->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 10);
$page->setStyle($footerStyle);
$page->drawText('Footer text', 33*MTP, 20*MTP, 'ISO-8859-1');
//save the page to pdf->pages
$pdf->pages[0] = $page;
//save the page as a template
$template = $pdf->pages[0];
//create the next page from template
$page1 = new Zend_Pdf_Page($template);
//save the next page to pdf->pages
$pdf->pages[] = $page1;
Have I completely misunderstood how this "template function" work in Zend_Pdf? I know that Zend_Pdf lacks quite a number of features compaired to some of the external pdf-generators (like FPDF) but still, there must be some way to make a basic header/footer for all pages right?
Try to use clone instead passing page as argument to a new page.
$page1 = clone $template;
or simply
$pdf->pages[] = clone $pdf->pages[0];

Draw text on a loaded pdf file with Zend Framework

I'm trying to load a existing pdf file, and fill this with database information. Loading the file and everything is working, except for writing data to the loaded page. It doesn't write text to the loaded page. If I add a new page en use a foreach to apply drawing to all pages, all added pages are written, except for the loaded one. Below is the code I'm using:
$pdf = Zend_Pdf::load('./documents/agreements/_root/gegevens.pdf'); // Load pdf
$pdf->pages = array_reverse($pdf->pages); // reverse pages
$pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4); // Add a page (A4)
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // Set font
foreach($pdf->pages as $page) // Apply settings+text to every page (total of 2)
{
$page->setFont($font, 36);
$page->setAlpha(0.25);
$page->drawText('LALALALALALALA', 62, 260, 'UTF-8');
}
$pdf->save('./documents/agreements/Gegevens_'.$this->school_id.'.pdf'); // Save file
I solved the problem: I created a new pdf file with different settings. Creating the pdf with the following settings (I use Acrobat PDFmaker Office COM Addin for word) did the trick. I guess the code was working after all, the pdf itself was causing the problems.
In word select save as PDF
Select the 'Quick and simple PDF' format
Change the settings in 'Adobe PDF conversion options':
Enable -> Convert document information
Enable -> Make PDF/A Compliant
Disable -> Create bookmarks from
Disable -> Convert Comments
Note: This regards saving a file as PDF in word. Other office applications aren't tested.
Try the following:
$pdf = Zend_Pdf::load('./documents/agreements/_root/gegevens.pdf'); // Load pdf
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // Set font
foreach($pdf->pages as $pid => $page) // Apply settings+text to every page (total of 2)
{
$myPage = new Zend_Pdf_Page($page);
$myPage->setFont($font, 36);
$myPage->setAlpha(0.25);
$myPage->drawText('LALALALALALALA', 62, 260, 'UTF-8');
$pdf->pages[$pid] = $page;
}
$pdf->save('./documents/agreements/Gegevens_'.$this->school_id.'.pdf'); // Save file
try this one.
$pdf->pages[i]->drawText('LALALALALALALA', 62, 260, 'UTF-8');