Change orientation of a pdf using zend pdf - zend-framework

I have an existing pdf, and i want to load it, change its orientation and save it. I tried this but it doesn't work :
$pdf = Zend_Pdf::load('mypdf');
$page = $pdf->pages[0];
$page->rotate(0,0,deg2rad(90));
$pdf->save('new.pdf');
Any idea?

You have to assign the page $page to the PDF document $pdf after you rotated the page:
$page->rotate(0,0,deg2rad(90));
$pdf->pages[0] = $page; // assign the page to the PDF document
$pdf->save('new.pdf');
Be aware that in the above example the old page 1 is overwritten in the $pdf object.

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();

Change page numbers in EvoPDF

Within a pdf it is possible to change page numbering, so the first page would be page 5, etc.
(This has nothing to do with headers and footers, i'm speaking strictly about the page numbers as they appear in the pdf toolbar)
Is it possible to control those numbers with EvoPDF?
Yes, apparently with EVOPDF v5 you can set the number to be displayed on the page using the PageNumberingStartIndex property of the PdfHeaderOptions object (same for Footers). I don't know of any examples using this.
It is not possible to change the page numbering displayed by Adobe Reader using an option in the generated PDF document. What you can do is to make the PDF viewer go to a certain page in PDF document when the document is opened. You can check the Go To a Location in a PDF Page When the Document is Opened Demo . The C# code to implement this feature is:
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
Document pdfDocument = null;
try
{
// Convert a HTML page to a PDF document object
pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);
int goToPageNumber = int.Parse(pageNumberTextBox.Text);
if (goToPageNumber > pdfDocument.Pages.Count)
{
return;
}
// Get destination PDF page
PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1];
// Get the destination point in PDF page
float goToX = float.Parse(xLocationTextBox.Text);
float goToY = float.Parse(yLocationTextBox.Text);
PointF goToLocation = new PointF(goToX, goToY);
// Get the destination view mode
DestinationViewMode viewMode = SelectedViewMode();
// Create the destination in PDF document
ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode);
// Set the zoom level when the destination is displayed
if (viewMode == DestinationViewMode.XYZ)
goToDestination.ZoomPercentage = int.Parse(zoomLevelTextBox.Text);
// Set the document Go To open action
pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination);
// Save the PDF document in a memory buffer
byte[] outPdfBuffer = pdfDocument.Save();
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Go_To_Page_Open_Action.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
finally
{
// Close the PDF document
if (pdfDocument != null)
pdfDocument.Close();
}
}

Could not generate thumbnail from tslib_cObj instance in typo3

I have tried following typo-script to generate thumbnail. It is not working for me.
Please help.
$cObj = t3lib_div::makeInstance('tslib_cObj');
$image_conf['file'] = 'uploads/pics/filename.jpg';
$image_conf['file.']['width'] = $width;
$image_conf['file.']['height'] = $height;
$newThumb = $cObj->IMAGE($image_conf);
print $newThumb;
I want to generate thumbnail from jpg,jpeg,png formats
In your plugin you can use
$this->cObj->IMAGE($image_conf);
to scale an image.
As the cObject has some dependencies, an instance of t3lib_div::makeInstance('tslib_cObj'); wont be sufficient to scale images.

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');