PhpPowerpoint set slide width and height (slide dimensions) - php

I want to know if there is a way to create a ppt file with pre defined width and height rather than default one.

I've used this code to set it for the new PHPPresentation (newer PHPPowerpoint version).
Hope it helps..(replace path's with your phppresentation path's
and width(1180) and height(768) to suit yours
/*Standard library loaders */
require_once 'include/Common/src/Common/Autoloader.php';
\PhpOffice\Common\Autoloader::register();
require_once 'include/PHPPowerPoint2/src/PhpPresentation/Autoloader.php';
\PhpOffice\PhpPresentation\Autoloader::register();
/*Standard library loaders */
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\DocumentLayout;
$objPHPPowerPoint = new PhpPresentation();
$objPHPPowerPoint->getLayout()->setDocumentLayout(DocumentLayout::LAYOUT_CUSTOM, true)
->setCX( 1180, DocumentLayout::UNIT_PIXEL)
->setCY( 768, DocumentLayout::UNIT_PIXEL);

The answer of #user2633993 is still valid, though the code for setting the layout width and height has changed a bit, now you need to set an array containing the cx and cy keys, their values doesn't matter.
So the code needs to look something like this:
$objPHPPowerPoint->getLayout()->setDocumentLayout(['cx' => 1280, 'cy' => 700], true)
->setCX(1280, DocumentLayout::UNIT_PIXEL)
->setCY(700, DocumentLayout::UNIT_PIXEL);`

you can set width and height:
Please see this tutorial
$objPHPPowerPoint = new PHPPowerPoint();
$currentSlide = $objPHPPowerPoint->getActiveSlide();
$shape = $currentSlide->createDrawingShape();
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(300);
$shape->setWidth(600);
$shape->setOffsetX(170);
$shape->setOffsetY(180);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER );
$textRun = $shape->createTextRun('Thank you for using PHPPowerPoint!');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(60);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFC00000' ) );
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx', __FILE__));

Related

Add an image to an PDF with form fields

I'm trying with PHP to fill form fields on an existing PDF and add an image to it.
I found the FPDM library to fill the form fields:
$formInputArray = ['field1' => 'test value', 'field2' => 'second value'];
$pdf = new FPDM('orginal-file.pdf');
$pdf->getEntries($templatePDF, 'PDF');
$pdf->Load($formInputArray);
$pdf->Merge();
$pdf->Output('F', 'form-filled-file.pdf');
That works so far.
In the next step i try to add an image with the Fpdi class to the edited document:
$pdf = new Fpdi();
$pdf->setSourceFile('form-filled-file.pdf');
$pageId = $pdf->importPage(1, \setasign\Fpdi\PdfReader\PageBoundaries::MEDIA_BOX);
$pdf->addPage();
$pdf->useTemplate($pageId);
$pdf->Image('test-image.jpg', 150*0.39, 150*0.39, 100*0.39);
$pdf->Output('F', 'finished-file.pdf');
The problem is, that Fpdi is converting the template pdf structure into an new pdf structure. So all given form fields are gone.
So the question is:
How can i add an image to an existing PDF with form fields?
I also looked at the iText / PDFtk (Serverside) and the mPDF PHP library, but they are not the right one because of the GPL license.
Is there an other way or an other library to fill form fields and add an image to an PDF in PHP?
We (Setasign - also author of FPDI) offer a commercial solution for both tasks: Filling PDF forms and fill fields with images in pure PHP.
If you'd used FPDM you was only able to fill in text fields. A replacement would be the SetaPDF-FormFiller Lite Component. The Full version would allow you to fill in also other field types such as checkboxes or radiobutton groups.
A simple example of filling a single text field and an additional field with an image would be:
<?php
require_once('library/SetaPDF/Autoload.php');
// or if you use composer require_once('vendor/autoload.php');
// create a file writer
$writer = new SetaPDF_Core_Writer_File('image-in-form-field.pdf');
// get the main document instance
$document = SetaPDF_Core_Document::loadByFilename($filename, $writer);
// now get an instance of the form filler
$formFiller = new SetaPDF_FormFiller($document);
// Get the form fields of the document
$fields = $formFiller->getFields();
// Let's fill a field
$fields['Text Field']->setValue("Some example text.");
// Now prepare an appearance for the Logo field
// First of all let's get the annotation of the form field
$annotation = $fields['Logo']->getAnnotation();
// Remember the width and height for further calculations
$width = $annotation->getWidth();
$height = $annotation->getHeight();
// Create a form xobject to which we are going to write the image.
// This form xobject will be the resulting appearance of our form field.
$xobject = SetaPDF_Core_XObject_Form::create($document, array(0, 0, $width, $height));
// Get the canvas for this xobject
$canvas = $xobject->getCanvas();
// Let's create an image xobject
$image = SetaPDF_Core_Image::getByPath('Logo.png')->toXObject($document);
// scale image into available space and align in the center
if ($image->getHeight($width) >= $height) {
$image->draw($canvas, $width / 2 - $image->getWidth($height) / 2, 0, null, $height);
} else {
$image->draw($canvas, 0, $height / 2 - $image->getHeight($width) / 2, $width);
}
// Now add the appearance to the annotation
$annotation->setAppearance($xobject);
// Flatten all appearances to the pages content stream
$fields->flatten();
// finish the document
$document->save()->finish();
This script is a short version of this demo.

How to set custom page size in fpdf php library?

I need some help with fPDF. I want to set up my custom page size (exactly: width 3 inch, and height 5 or 6 inch).
it will create number of pages again height parameter .
i set the size array(3,5). it will create 5 page. I
found fPDF() manual (http://www.fpdf.org/) but there are only ready formats like A4, B5 etc. I have to set up my own page format.
<?php
require_once('fpdf/fpdf.php');
//$fromat = array(3,5);
$pdf = new FPDF('p','in', [4.1,2.9]);
$pdf->SetTopMargin(50);
$pdf->Addpage();
$pdf->SetTitle("invoice");
$pdf->SetCreator("maqbool solutons");
$pdf->SetAuthor("my name");
$pdf->SetSubject("report");
$pdf->SetFont('Arial', 'B', '16');
$pdf->SetTextColor(155,14,9);// rgb
$pdf->SetDrawColor(155,14,9);
$pdf->SetfillColor(15,140,95);
$pdf->Cell(60,10, 'hello word');
$pdf->Cell(60,10,'powered by fpdf', 1, 0,'c',true);
$pdf->Cell(60,10,'powered by fpdf', 1, 2,'c');
$pdf->Cell(60,10,'powered by fpdf', 1, 1,'c');
$pdf->Image("images/coat.jpg", 10,20,10,35);
$pdf->MultiCell(94,10,"skldjfsldfsfjsdkfsjdlfjsdflkjsdflksjflksjdflskjfslkjfdslkfdjslkfdjslkfjslkfjslkfjsflkjsflkjsflksjflksjfslkjfslkjslkf",1,"L",false);
$pdf->Output("I", "invice.pdf");
?>[that is my file size][1]
when i add array of size
You should should define it in your constructor like so:
$pdf = new FPDF('P','in',[3,6]);
You can find more info in tutorial #1 and in the manual > AddPage
As said in the documentation, when you call the constructor or AddPage, you can either give a String or an Array containing the width and height:
// AddPage([string orientation [, mixed size [, int rotation]]])
$pdf->AddPage("P", [3, 5]); // assuming you are using 'in' as unit
Or directly using the constructor:
// __construct([string orientation [, string unit [, mixed size]]])
$pdf = new FPDF('P','in',[3, 5]);
I think you can set the page size with the constructor.
I have not tested it but this should show you the way:
$format=array(3,5);
$pdf=new FPDF('P','in',$format);
$pdf->Open();
....

How to make pdf exact 10CM width with DOMPDF and Laravel?

I am creating a PDF with DOMPDF and laravel.
The pdf is being printed with a special printer that only accepts files with 10CM width and 20CM height.
I have tried this:
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper);
Since 11X17 is
"11x17" => array(0, 0, 792.00, 1224.00),
I figured 10X20 was 0,0720,1440
But it's not working.
Any help is appreciated.
Thanks in advance
How i fixed this:
change the custom paper.
Download the PDF open in Acrobat Reader move your mouse to the left corner now you can see the width and height of the document, and i changed the custom paper accordingly.
The end result was:
10CM X 20CM =
$customPaper = array(0,0,567.00,283.80);
$pdf = PDF::loadView('pdf.retourlabel', compact('retour','barcode'))->setPaper($customPaper, 'landscape');
Very circuitous work but i did get the Job done..
Thanks
Setting the paper size in PHP requires knowing the point (pt) measurement, points being the native PDF unit. The PDF resolution (with Dompdf) is 72pt/inch. So 10cm x 20cm is roughly equivalent to 283 X 566 (as you noted in your answer).
1 inch = 72 point
1 inch = 2.54 cm
10 cm = 10/2.54*72 = 283.464566929
20 cm = 10/2.54*72 = 566.929133858
landscape is mean opposite. So, we can set it like : array(0, 0, 566.929133858, 283.464566929 ) which same as the answer but in more precise value
You can, however, allow Dompdf to calculate the appropriate point size by specifying your page dimensions in CSS. This is available starting with Dompdf 0.6.2.
<html>
<head>
<style>
#page { size: 10cm 20cm landscape; }
</style>
</head>
<body>
...
</body>
</html>
Trivia: the PDF spec does not provide for a method of indicating paper orientation (though there is a method of indicating a rotation). Dompdf just flips the width/height when landscape orientation is specified.
I think the issue is with orientation, since the setPaper uses 'A4' orientation as default so this might be the reason that your code is not working.
/**
* Set the paper size (default A4)
*
* #param string $paper
* #param string $orientation
* #return $this
*/
public function setPaper($paper, $orientation = 'portrait'){
$this->paper = $paper;
$this->orientation = $orientation;
$this->dompdf->setPaper($paper, $orientation);
return $this;
}
Try using :
$customPaper = array(0,0,720,1440);
$pdf = PDF::loadView('pdf.retourlabel',compact('retour','barcode'))->setPaper($customPaper,'portrait');
Hope that helps or try other options too instead of portrait.

can't create many pdf files using dompdf

I'm trying to create many pdf files using a loop and put them in a specific place, but I'm getting this error:
DOMPDF_Exception
File:
C:\wamp\www\leadmarket\vendor\dompdf\dompdf\include\inline_positioner.cls.php:37
Message:
No block-level parent found. Not good.
Here is my code:
for($i = 0 ; $i < 3 ; $i++){
$pdf = new PdfModel();
$pdf->setOption("paperSize", "a4"); //Defaults to 8x11
$pdfView = new ViewModel($pdf);
$pdfView->setTerminal(true)
->setTemplate('application/annonceur/generer-facture-mensuelle.phtml')
->setVariables(array(
'client' => $i
)
);
$html = $this->getServiceLocator()->get('viewpdfrenderer')->getHtmlRenderer()->render($pdfView);
$eng = $this->getServiceLocator()->get('viewpdfrenderer')->getEngine();
$eng->load_html($html);
$eng->render();
$pdfCode = $eng->output();
file_put_contents('public/folder/file-'.$i'.pdf', $pdfCode);
}
Dompdf currently only supports rendering a single document per instance (issue #1056). Until this issue is resolved you'll need to unset the Dompdf instance and create a new one.
It looks like you're creating an instance per loop of your PdfModel and ViewModel, but the Dompdf instance is part of the class that contains your loop ($this->getServiceLocator...).
I'm not familiar with the framework you're using, so I can't give you a specific answer. The quickest solution would be to not use the Dompdf instance that's part of your ServiceLocator instance.
Replace
$eng = $this->getServiceLocator()->get('viewpdfrenderer')->getEngine();
With
$eng = new Dompdf();
$eng->setOption("paperSize", "a4");
As you see the paper size had to be set again, and that's the drawback. Any options configured in the ServiceLocator will be lost.

How to rotate full page with tcpdf?

I am using TCPDF library to write a custom size label with background image and multiple text blocks on it.
The user when sees the preview on screen of the PDF it should show in horizontal, but for printing, I need the full page rotated -90 degrees.
How can I just rotate the whole page for printing version without having to move anything?
Basically:
In my case I've already had to use a new document format for the special sizes my document required.
So I've duplicated that format, created one for Landscape and one for Portrait.
Then based on the $preview variable, if previewing I'm rendering the normal landscape document, but if not previewing, I'm using the Portrait format and orientation and also starting the transformation and rotating everything on page.
Hope this helps someone I've found no other "quick" way to accomplish this kind of full-page rotation.
<?php
// #1 get the preview attribute from
// the form that was submitted from the user
$preview= isset($_POST['preview'])?(int)$_POST['preview']:0;
// load TCPDF for CodeIgniter as a library
$this->load->library('Pdf');
// #2 set default orientation and format
$orientation='L';
$format='MAKE-L';
// #3 if not previewing, switch orientation and format to portrait
if (!$preview) {
$orientation='P';
$format='MAKE-P';
}
// create new pdf object
// (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
$pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage($orientation, $format);
// #4 if not previewing, start transformation
// and rotate everything before inserting any content
if (!$preview) {
// Start Transformation
$pdf->StartTransform();
// Rotate counter-clockwise centered by (x,y)
$pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
}
// put your content here,
// for example set font and add a text
$pdf->SetFont('times', '', 7, '', true);
$pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
/// end content
// #5 if not in preview mode, finish the transformation
if (!$preview) {
// Stop Transformation
$pdf->StopTransform();
}
$pdf->Output('example.pdf', 'I');
/**
* Last but very important note:
* I have added my formats in tcpdf/includes/tcpdf_static.php file.
* >> MAKE-L for Landscape
* >> MAKE-P for Portrait
*/
public static $page_formats = array(
// Make
'MAKE-L' => array( 396.850, 425.196), // = ( h 140 x w 150 ) mm
// Make
'MAKE-P' => array( 425.196, 396.850 ), // = ( h 140 x w 150 ) mm
// .. rest of formats here ../
);
The setPageFormat() method should do the job. You also can pass the parameter to the $format parameter of AddPage():
$pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);

Categories