Hy,
I have some images that I converto to PDF pages with TCPDF::Image, and then I add the text on another level, but the results I get rendering text with TCPDF are quite awful, this is how the text should look
And this is the rendered PDF
As you can see TCPDF is inserting some new line "randomly", And I can't understand why, I'm using tcpdf 6.2.6
this is my code
$pdf->SetFont($TCPDFFontFile, $fontStyle, $fontSize, '', false);
$pdf->setTextRenderingMode(0, true);
$pdf->setCellHeightRatio($lineHeight);
$pdf->SetTextColorArray($textColorArray);
$pdf->MultiCell(
$removedItem->width,
$removedItem->height,
$removedItem->text,
0,
$textAlign,
false,
1,
$left,
$right
);
Any ideas? Maybe a problem with the latest version? Even if I already tried with many other version like 6.1.0 or 6.2.0 but nothing changed...
EDIT: I noticed that there are differences even in the charater spacing, and even the height of the lines are slithly different, is this any help?
Related
We have an application that takes text from a form (it's ultimately a spreadsheet) and pulls it from the database and creates a pdf document. Using fdpf and everything is working well. But there is a header portion of the doc that is currently plain text posting from a <texarea>, and we want to upgrade that portion to rich text using tinymce. I can easily use https://github.com/spipu/html2pdf to create a nice, full doc from the text, but it's only meant to be a small portion of the doc. This heading is not the "Header", which is taken from a jpg image.
I've tried several approaches including creating a separate doc with the rich text (html) portion and attempting to merge the two, which works great if the two are separate pages, but not so well when trying position one inside another when the one is of varying sizes, shapes, lines. Another thing I've tried is using imagemagick to convert the pdf taken from the rich text portion to a jpg and then inserting the image into the other pdf. This works great unless the heading contains mostly text(as it will).Images come through well but text becomes unreadable with my lack if image manipulation prowess.
using fpdf
class foo extends FPDF
{
public function fooBody ($bar) {
`````````````````````````````````
$data is obj containing db entities
current lines for the heading (having to remove html rn)
$data->heading = preg_replace("/ /",'',$data->heading);
$this->MultiCell(190, 3, trim(strip_tags($data->heading)), 0, 'L', 0);
$foo= new foo();
$foo->fooBody(bar);
add page...
output()..
etc.
wish I could just do something like below
$html2pdf->writeHTML($data34->heading);
$richTextHeading = $html2pdf->output('file.pdf', 'S');
foo->MultiCell(190, 3,$richTextHeading, 0, 'L', 0);
I want to use the same doc I have now with fpdf but somehow include some converted html for a small portion of it, but am currently only capable of having one (current doc) or the other (html doc).
If anyone comes across this thread, I ended up just using TCPDF, and re-writing my old code to work with TCPDF. Suprisingly I only need to change font names in a few places (Ariel->helvetica) and explicitly state cell height and margins. The old code worked mostly as is, and this
$this->MultiCell(190, 3, trim(strip_tags($data->heading)), 0, 'L', 0);
Is now simply this
$this->writeHTML( $data->heading);
We have an Image processing microservice that created rich images with text on top of it and we are in the process of adding arabic locale to our website
While translating some of the content to Arabic our translator told us that the text in generated images not rendering correctly in arabic - they appear with no ligatures, so every character is separated.
We currently use ImageMagick (6.8.9-9) with PHP and the text generation used ImagickDraw->annotateImage that worked fine until we hit that ligatures problem.
I googled a little and found "pango" that solves that problem and also allows some sort of "html" syntax to define multiple settings for one text line which is pretty cool.
Problem is that I didn't find a way to use custom font files
This is the code:
$img = new \Imagick;
$img->newImage($width, $height, new ImagickPixel('transparent'));
$img->setBackgroundColor(new ImagickPixel('transparent'));
$img->setFont($fontFile); // didn't work
$img->setPointSize($fontSize);
$img->setOption("font", $fontFile); // didn't work
$img->newPseudoImage($width, $height, "pango:<span font='".$fontName."' foreground='".$textColor."'>".$text."</span>"); // font='".$fontName."' - also didn't work
I also tried to install the font in ubuntu OS and use only the font name but that didn't worked either, as well as combining all the options above or some of them.
I found that question:
PHP Imagick don't use custom font > convert does. But he said he used "caption" instead of pango - which doesn't provide that cool "html" syntax and does not solve the Arabic issue.
Please - if you know any solution I will love you forever!
Thanks.
I found annoying error with TCPDF library. Hope you know how help me. I use obj->getNumLines method to calculate how many lines has my text. It works all good in most cases but unfortunatelly not always.I use UTF8 encoding and I use many local signs (I'm from poland and we have a lot of such signs). getNumLines sometimes returns i.e. 5 although multicell displays 6 lines of text.
In my research I found that:
$font = $pdf->addTTFfont('/fonts/calibri.ttf', '', '', 32);
$pdf->setFont($font, '', 13)
$string = 'zażółć gęślą jaźń'; (repeated many times)
echo $pdf->getNumLines($string, 0);
It prints to the screen different (smaller) number than multicell actually returns lines to the pdf file. I use custom fonts as you can see above, margins are not changed during the code.
Does anybody has idea what is wrong or how to fix it?
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.
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.
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
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);
I've run into another problem with GD and PHP.
I'm successfully writing text to an image.
However, I've encountered a case where it would be beneficial to place the text - instead of directly on the image - on a rectangle (or any shape) to create a solid background for the text where the image it's being placed on might not allow the text to be read very easily.
My two ideas are, in order of preference:
Fill the background with the color as it writes the text
Write the text to an appropriately sized image with a solid background, and then overlay the image onto the target
I can't figure out how to do #1 and #2 seems overly complex and I don't know how to determine the dimensions of the text so that I can create a new image for it.
For clarity, here is the output that isn't very good:
And here's how I'd like it to look, with a tight box behind the text of any color:
I'm open to any suggestions, but drawing the color on the fly without any other images or hackiness would obviously be my first choice.
Update:
After #Dan suggested using `imagettftext', I decided that it was high time I added support for that function to my library. Everything is working as would be expected except for one major issue.
The text that's written to the image is still transparent, even when written to a solid background (0 transparency).
Here's a script I wrote to test:
<?php
set_include_path('backbone:global:jquery');
require_once('Image.php');
$scout = new Image();
$scout->source = "scout.jpg";
$result = $scout->Write->Font(25, 25, "A Fairly Long String", 12, "#FF0000", 0, "LiberationSans-Regular.ttf", 1, "#FFFF00", .4, 4);
if( !isset($_GET['dev']) )
{
header("Content-Type: " . $scout->contentType());
}
if( $result )
{
$scout->output();
}
?>
The files I used/required:
1. scout
2. liberation font
3. Image Manipulation Library
- Image
- ImageBase
- ImageCombine
- ImageDraw
- ImageManipulate
- ImageWrite
I apologize about all the files, it really only uses Image, ImageBase, ImageCombine, and ImageWrite, but the others are require_onceed by the loader.
Here's a sample of the output from the script above:
And here's output with zero transparency (fully opaque):
$result = $scout->Write->Font(25, 25, "A Fairly Long String", 12, "#FF0000", 0, "LiberationSans-Regular.ttf", 1, "#FFFF00", 1, 4);
Any ideas what could be causing this? It's EXTREMELY possible that it's my code somewhere, but it seems strange that it would work exactly as I thought it should except for this one bug.
In searching desperately for the answer to this problem, I stumbled upon this SO question that's tangentially related to my problem, and contains the answer.
I've never fully understood why you tell imagealphablending false when you want transparency, so I guess my failure to properly understand the code I'm using has led to this issue.
In any case, the following modified code works like a charm in my one single test case.
To write text to a background without forced 100% transparency for the character boxes, you must turn alpha blending ON while writing the text:
imagealphablending($text->handle, true);
$bool = imagettftext($text->handle, $textSize, $angle, $padding, abs($size[5]) + $padding, $this->AllocateColor($rgba[0]['r'], $rgba[0]['g'], $rgba[0]['b'], $rgba[0]['alpha']), $font, $string);
imagealphablending($text->handle, false);
Maybe this will do the job for you if you switch to ttf. http://php.net/manual/en/function.imagettfbbox.php