mPDF importing page from another document - php

I'm trying to create PHP document in PHP using mPDF library. I want to import the last page from another PDF document. According to document I use, the imported page is too small (it's on 1/3 of the page) or too big (I see only part of the page). Is there any way to make it fit?
My code:
$mpdf = new mPDF('', '', 0, '', 0, 0, 0, 0, 0, 0, 'L');
$mpdf->WriteHTML($html);
$mpdf->AddPage();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile('application/views/default/pdf/nabidka-design.pdf');
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->Output();
Thanks for help

You have to set page format for output PDF file like this:
$mpdf = new mPDF('utf-8', 'A4', '8', '', 10, 10, 7, 7, 10, 10);
Change 'A4' on your PDF source file format.

Related

SetDrawColor() is behaving like SetFillColor()

SetDrawColor() supposed to color Cell frame but instead another color is rendering as background color. This is the whole code :-
$pdf = new FPDF();
$pdf->SetTextColor(103, 58, 183);
$pdf->SetDrawColor(0, 80, 180);
// Fourth Page --
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0, 10, "Hello Santanu", 1, 1, 'C', true);
$pdf->Output('helo.pdf', 'D');
You should try using SetFillColor before drawing any cell because it's taking black as default.
$pdf->SetFillColor(0, 0, 0);

Image to PDF with tcpdf

Hy
I'm trying to convert an image (both png or jpeg) to pdf and then adding crop marks using tcpdf (no limitation on the library, it just seemed the best to me, no problems in changing if would solve the problem).
the best case for me is: I have an input image, I give it to some function of tcpdf, that converts it into a pdf, add the bookmarks in the angles (top-left, top-right, bottom-left, bottom-right) and save the pdf without nowing anything about the sizes of the image...
I tried looking around but coudn't find anything without passing the size of the images to tcpdf, this is what I came up with:
$pagelayout = array('706', '606'); // or array($height, $width)
$pdf = new TCPDF('', 'pt', $pagelayout, true, 'UTF-8', false);
// set document information
$pdf->SetAuthor('JOIN SRL');
// add a page
$pdf->AddPage();
$pdf->Image($input, 0, 0, '', '', 'JPG', 'http://www.tcpdf.org', '', true, 300, '', false, false, 1, false, false, false);
$pdf->Image($input);
$pdf->cropMark('', '', 10, 10, 'TL');
$pdf->cropMark('', '', 10, 10, 'TR');
$pdf->cropMark('', '', 10, 10, 'BL');
$pdf->cropMark('', '', 10, 10, 'BR');
$pdf->Output($output, 'F');
as you can see I had to pass the image size to tcpdf, but this is just a test, I would like to get rid of this informations in this peace of code...
And this dosn't work either because the pdf page comes otu bigger than the image, and hte crop marks don't get show( I think this is quite obvious, I tried not giving coordinates hoping that they would automatically set in the angles of the page, but no luck) .
Does someone have any ideas?
thank's
EDIT
I managed to solve nearly everything. could be fine like this( the code follows) but I wanted to know if there is a way to do the same thing without knowing the image size.
$pdf->setMargins(0, 0, 0);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
$pdf->Image($input);
$width = $pdf->getPageWidth();
$height = $pdf->getPageHeight();
$pdf->cropMark(5, 5, 5, 5, 'TL', array(255, 0, 0));
$pdf->cropMark($width - 5, 5, 5, 5, 'TR', array(255, 0, 0));
$pdf->cropMark(5, $height - 5, 5, 5, 'BL', array(255, 0, 0));
$pdf->cropMark($width - 5, $height - 5, 5, 5, 'BR', array(255, 0, 0));
$pdf->Output($output, 'F');
thank's

FPDF - Drawing a line that's centred across the width

Im trying to produce a PDF document using the PHP FPDF library,
Im trying to draw a line horizontal over the page which is indented the same amount both on the left and right sides.
Im having real difficulty trying to accomplish this.
My code is as follows, any help would be greatly appreciated.
$pdf = new FPDF( 'P', 'mm', 'A4' );
$pdf->AddPage();
$pdf->SetDisplayMode(real,'default');
$pdf->SetFillColor(0,0,0);
$pdf->SetFont('Arial','B',16);
$pdf->Image('logo.jpg',20,10,50,33.3);
$pdf->SetDrawColor(188,188,188);
$pdf->Line(20,45,150,45);
Given a portrait, A4 page is 210mm wide, a simple bit of maths should help you resolve this:
$pdf->Line(20, 45, 210-20, 45); // 20mm from each edge
$pdf->Line(50, 45, 210-50, 45); // 50mm from each edge
This is given that your declaration is as you stated in your original question:
$pdf = new FPDF( 'P', 'mm', 'A4' ); // A4, portrait, measurements in mm.
Use the following -
$pdf = new PDF('P','mm','A4'); //Set PDF as Potrait
$pdf->Ln(4); //Break
$pdf->Line(startpoint, 45, endpoint-50, 45); //Set the line
$pdf->Ln(4); //Break

How to add a stroke to only some characters in a string using PHP ImageMagick?

Say I have the following code:
$draw = new ImagickDraw(); // prep text
$draw->setFillColor('#00ff00');
$draw->setFontSize(12);
$draw->setStrokeWidth(4);
$draw->setStrokeColor(new ImagickPixel('#ff0000'));
// etc.
$image = new Imagick(); // prep image
// etc.
// add text to image
$image->annotateImage($draw, 10, 10, 0, 'Hello, World!');
And let's assume that it works (which it does - I've just cut it down here). Is there a simple way for me to, say, change the stroke width on a per-character basis?
I'm looking into using something like $image->queryFontMetrics($text, 'H')) but wondered if there was a simpler way.
Thanks!
$draw = new ImagickDraw(); // prep text
$draw->setFillColor('#00ff00');
$draw->setFontSize(12);
$draw->setStrokeColor(new ImagickPixel('#ff0000'));
// etc.
$image = new Imagick(); // prep image
// etc.
// add text to image
$draw->setStrokeWidth(4);
$image->annotateImage($draw, 10, 10, 0, 'He');
$draw->setStrokeWidth(5);
$image->annotateImage($draw, 12, 10, 0, 'llo, ');
$draw->setStrokeWidth(6);
$image->annotateImage($draw, 15, 10, 0, 'Wor');
$draw->setStrokeWidth(7);
$image->annotateImage($draw, 18, 10, 0, 'ld!');
Would something like that work or would that be too clunky? It could work if the string was always the same and you figured out what the $x dimensions were correctly (the 2nd parameter in the annotateImage() function)

Inserting an image with PHP and FPDF

I'm trying to insert an image but do not want to specify the x and y coordinates. Is that possible?
$pdf->Image($image1, 5, 70, 33.78);
I want to be able to specify the size (33.78) but not the x and y so that it moves based on the content.
$pdf->Write( 70, $reportTitle );
$pdf->Ln( 45 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $reportSubtitle );
/**
Create product 1
**/
$pdf->Ln( 10 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $prod1title );
$pdf->Ln( 30 );
$pdf->SetFont( 'Arial', '', 10 );
$pdf->Write( 5, $prod1sub );
$pdf->Ln( 30 );
$pdf->Image($image1, 5, 70, 33.78);
The above is the code I use. If $reportSubtitle is two or three lines, it pushes $prod1title and $$prod1sub down, and inevitably under the image that is fixed. Is there no way to have the image act like the product title and subtitle and be pushed down too while still declaring the size?
I figured it out, and it's actually pretty straight forward.
Set your variable:
$image1 = "img/products/image1.jpg";
Then ceate a cell, position it, then rather than setting where the image is, use the variable you created above with the following:
$this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );
Now the cell will move up and down with content if other cells around it move.
Hope this helps others in the same boat.
You can use $pdf->GetX() and $pdf->GetY() to get current cooridnates and use them to insert image.
$pdf->Image($image1, 5, $pdf->GetY(), 33.78);
or even
$pdf->Image($image1, 5, null, 33.78);
(ALthough in first case you can add a number to create a bit of a space)
$pdf->Image($image1, 5, $pdf->GetY() + 5, 33.78);
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
Please note that you should not use any png when you are testing this , first work with jpg .
$myImage = "images/logos/mylogo.jpg"; // this is where you get your Image
$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);
// Image URL
$url = 'img/img.png';
// Place the image in the pdf document
$pdf->Cell(30, 30, $pdf => Image($url, 5, $pdf => GetY(), 93.78), 0, 0, 'L', false);
The 93.78 is the size of the image.
5 is the position from the left side.
You can't treat a PDF like an HTML document. Images can't "float" within a document and have things flow around them, or flow with surrounding text. FPDF allows you to embed html in a text block, but only because it parses the tags and replaces <i> and <b> and so on with Postscript equivalent commands. It's not smart enough to dynamically place an image.
In other words, you have to specify coordinates (and if you don't, the current location's coordinates will be used anyways).

Categories