Question

I have a PHP project where I am opening up a premade PDF, and filling it out with data via PHP. The problem I am having is that one of the text elements isn't showing up. I am positioning it towards the bottom right of the PDF page. If I move it to the left a little, it shows up. It's as if there is some clipping or something.

I am using TCPDF, and since I am needing to modify an existing PDF, I am also having to use the FPDI class. It appears to me that FPDI normally is integrated with FPDF, so I've been using the FPDF methods to build out my PDF. OK, so here is some of my code (or the relevant parts)...

$pdf = new PDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetXY(261,200);
$pdf->Write(5, 'test');

There is at least a centimeter of whitespace to the right of the text when I position the text with a value of 260. If I move it just one more unit to 261, like in the code above, it just disappears. I'm able to position the text so far on the bottom of the page, that only the top half of the letters show, however, I can't even approach the right side of the page, or the text will completely disappear. I've set the SetAutoPageBreak to false, so new pages aren't created, and I've also flirted with zeroing out the margins.

Was it helpful?

Solution

I've had the best luck using cells to position text, for some reason they have shown to be more accurate and easier to work with than simply writing text onto the document:

$pdf->SetXY(261,200);
$pdf->Cell(0,10,'My text',0,1, 'C');

Docs: http://www.fpdf.org/en/doc/cell.htm

OTHER TIPS

Might be a bit late in the game...

I had a look at the fpdf.php file and it looks like it adds a margin of 1cm.

If you lower it then you can get text closer to the edge of the page. Below is the original line:

// Page margins (1 cm)
$margin = 28.35/$this->k;

But if you change it to something like

$margin = 10/$this->k;

This gets you closer to the edge of your document.

Write() is used for flowing text (internally it uses several Cell() calls). If it reaches the right margin an automatic line break is done and the next word/character will start a new line at the left margin. The word will not disappear but will be shown at the lower left area. You can see the characters flowing with this simple script:

$pdf = new FPDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetFont('Helvetica');
$pdf->SetXY(261,200);
$pdf->Write(5, 't e s t');
$pdf->Output();

Try the following:

$pdf->SetAutoPageBreak('auto',0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top