using italic Arial (embedded) in FPDI - php

I'm trying to output a pdf file with an italic text in Arial and using FPDI for that.
Since the arial.php and the arialbd.php (bold) are already existing, it's working fine.
Now I found a ariali.ttf on the internet, which looks good when I open the overview of that font (little window with the quick brown fox sentence). But when I use the font in FPDI, it looks like this:
http://i.stack.imgur.com/muTN7.png
Does anybody know, how to fix this?
Is it an ttf issue or where does that strange presentation come from?
Here my example code:
$pdf = new FPDI('P', 'mm', array(210, 297));
$pdf->SetAutoPageBreak(false);
$pdf->AddFont('Arial', 'I', 'ariali.php');
$pdf->AddFont('Arial', '', 'arial.php');
$pdf->AddPage();
$pdf->SetFont("Arial", "I", 16);
$pdf->SetXY(20,20);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(20,5,"This is an italic test text in Arial!");
$pdf->Output("test.pdf", "I");
EDIT:
test.pdf
Setasign's question gave me the hint to try to open the resulting PDF in another PDF viewer too than just in my browser (chromium on debian).
The GNOME document viewer Evince is showing a blank page and Adobe Acrobat on Windows is showing nice italic Arial text but giving the error (freely translated from German) "The embedded font "Arial-ItalicMT" could not be taken out. In certain circumstances, some characters won't be printed correctly"
I'm just using my example code and FPDI Version 1.4.4.

The problem was the converter I used to generate the .php and the .z file from an .ttf file.
Always use the makefont scripts, shipped with fpdf/fpdi or use the online makefont: http://www.fpdf.org/makefont/

Related

How to add new CoreFont to FPDF?

I have tried to add a Font into fpdf by adding this
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->AddFont('calibri','','calibri.php');
$pdf->AddFont('calibri','B','calibrib.php');
$pdf->AddFont('DejaVuSansMono','','DejaVuSansMono.php');
$pdf->AddFont('DejaVuSansMono','B','DejaVuSansMono-Bold.php');
If I use the font courier
$pdf->SetFont('courier', '', 10);
$pdf->Cell(0, 5, 'Hello world', 0, 1);
and preview the generated pdf in the browser, everything is fine.
If I use e.g. calibri the pdf has to be loaded twice to see the result. On first time it comes with an error like:
Error while loading the PDF-Document
If I load it again, it works and if I load it again it wont work and so on...
What could this be?
Could it be that the font file is to large ?
EDIT:
I have to say, this only appears when I call the PDF from my Preview-Script whats actually is build with jquerys ajax. Adding images to the pdf (e.g. 20MB) does not bring up this problem.
And if I preview, open up the pdf in a other tab, and go back to the first tab and load prieview again also dont brings up the problem.

Yii TCPDF missing embed font in Corel Draw X6

I'm using this extension in Yii:
http://www.yiiframework.com/extension/tcpdf/
I would like to write to a pdf file with an external font, for example this:
http://www.1001fonts.com/open-sans-font.html
Here is my code:
$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Some Body");
$pdf->SetTitle("TCPDF Example 002");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$fontRegular = $pdf->addTTFfont(Yii::getPathOfAlias('webroot') . '/OpenSans-Regular.ttf');
$pdf->SetFont($fontRegular, "", 30);
$pdf->Cell(0,10,"Example",1,1,'C');
$pdf->Output("example_002.pdf", "I");
It works, I can read the pdf many programs, except Corel Draw 6X. When I'm opening the pdf it starts with a message of "missing fonts" and I can't replace them any other kind of fonts. Can anyone tell me how should I add the ttf font, or embed it into the pdf?
Thanks in advance!
So the problem is for Corel Draw . You have to Install the font.
1)Download your ttf font from here
2)Extract it and copy the font to C:\Windows\Fonts
3) Restart Corel Draw and make sure the new font is available by creating a text field and selecting the font.
Reference

pdf editor that integrated with PHP [duplicate]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know of a good method for editing PDFs in PHP? Preferably open-source/zero-license cost methods. :)
I am thinking along the lines of opening a PDF file, replacing text in the PDF and then writing out the modified version of the PDF?
On the front-end
If you are taking a 'fill in the blank' approach, you can precisely position text anywhere you want on the page. So it's relatively easy (if not a bit tedious) to add the missing text to the document. For example with Zend Framework:
<?php
require_once 'Zend/Pdf.php';
$pdf = Zend_Pdf::load('blank.pdf');
$page = $pdf->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
$page->drawText('Hello world!', 72, 720);
$pdf->save('zend.pdf');
If you're trying to replace inline content, such as a "[placeholder string]," it gets much more complicated. While it's technically possible to do, you're likely to mess up the layout of the page.
A PDF document is comprised of a set of primitive drawing operations: line here, image here, text chunk there, etc. It does not contain any information about the layout intent of those primitives.
There is a free and easy to use PDF class to create PDF documents. It's called FPDF. In combination with FPDI (http://www.setasign.de/products/pdf-php-solutions/fpdi) it is even possible to edit PDF documents.
The following code shows how to use FPDF and FPDI to fill an existing gift coupon with the user data.
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile('gift_coupon.pdf');
// import page 1
$tplIdx = $this->pdf->importPage(1);
//use the imported page and place it at point 0,0; calculate width and height
//automaticallay and ajust the page size to the size of the imported page
$this->pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
// now write some text above the imported page
$this->pdf->SetFont('Arial', '', '13');
$this->pdf->SetTextColor(0,0,0);
//set position in pdf document
$this->pdf->SetXY(20, 20);
//first parameter defines the line height
$this->pdf->Write(0, 'gift code');
//force the browser to download the output
$this->pdf->Output('gift_coupon_generated.pdf', 'D');
If you need really simple PDFs, then Zend or FPDF is fine. However I find them difficult and frustrating to work with. Also, because of the way the API works, there's no good way to separate content from presentation from business logic.
For that reason, I use dompdf, which automatically converts HTML and CSS to PDF documents. You can lay out a template just as you would for an HTML page and use standard HTML syntax. You can even include an external CSS file. The library isn't perfect and very complex markup or css sometimes gets mangled, but I haven't found anything else that works as well.
Don't know if this is an option, but it would work very similar to Zend's pdf library, but you don't need to load a bunch of extra code (the zend framework). It just extends FPDF.
http://www.setasign.de/products/pdf-php-solutions/fpdi/
Here you can basically do the same thing. Load the PDF, write over top of it, and then save to a new PDF. In FPDI you basically insert the PDF as an image so you can put whatever you want over it.
But again, this uses FPDF, so if you don't want to use that, then it won't work.
Zend Framework can load and edit existing PDF files. I think it supports revisions too.
I use it to create docs in a project, and it works great. Never edited one though.
Check out the doc here
The PDF/pdflib extension documentation in PHP is sparse (something that has been noted in bugs.php.net) - I reccommend you use the Zend library.
Tcpdf is also a good liabrary for generating pdf in php
http://www.tcpdf.org/
We use pdflib to create PDF files from our rails apps. It has bindings for PHP, and a ton of other languages.
We use the commmercial version, but they also have a free/open source version which has some limitations.
Unfortunately, this only allows creation of PDF's.
If you want to open and 'edit' existing files, pdflib do provide a product which does this this, but costs a LOT
<?php
//getting new instance
$pdfFile = new_pdf();
PDF_open_file($pdfFile, " ");
//document info
pdf_set_info($pdfFile, "Auther", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Creator", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Title", "PDFlib");
pdf_set_info($pdfFile, "Subject", "Using PDFlib");
//starting our page and define the width and highet of the document
pdf_begin_page($pdfFile, 595, 842);
//check if Arial font is found, or exit
if($font = PDF_findfont($pdfFile, "Arial", "winansi", 1)) {
PDF_setfont($pdfFile, $font, 12);
} else {
echo ("Font Not Found!");
PDF_end_page($pdfFile);
PDF_close($pdfFile);
PDF_delete($pdfFile);
exit();
}
//start writing from the point 50,780
PDF_show_xy($pdfFile, "This Text In Arial Font", 50, 780);
PDF_end_page($pdfFile);
PDF_close($pdfFile);
//store the pdf document in $pdf
$pdf = PDF_get_buffer($pdfFile);
//get the len to tell the browser about it
$pdflen = strlen($pdfFile);
//telling the browser about the pdf document
header("Content-type: application/pdf");
header("Content-length: $pdflen");
header("Content-Disposition: inline; filename=phpMade.pdf");
//output the document
print($pdf);
//delete the object
PDF_delete($pdfFile);
?>

Modify first page of PDF file in PHP [duplicate]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know of a good method for editing PDFs in PHP? Preferably open-source/zero-license cost methods. :)
I am thinking along the lines of opening a PDF file, replacing text in the PDF and then writing out the modified version of the PDF?
On the front-end
If you are taking a 'fill in the blank' approach, you can precisely position text anywhere you want on the page. So it's relatively easy (if not a bit tedious) to add the missing text to the document. For example with Zend Framework:
<?php
require_once 'Zend/Pdf.php';
$pdf = Zend_Pdf::load('blank.pdf');
$page = $pdf->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
$page->drawText('Hello world!', 72, 720);
$pdf->save('zend.pdf');
If you're trying to replace inline content, such as a "[placeholder string]," it gets much more complicated. While it's technically possible to do, you're likely to mess up the layout of the page.
A PDF document is comprised of a set of primitive drawing operations: line here, image here, text chunk there, etc. It does not contain any information about the layout intent of those primitives.
There is a free and easy to use PDF class to create PDF documents. It's called FPDF. In combination with FPDI (http://www.setasign.de/products/pdf-php-solutions/fpdi) it is even possible to edit PDF documents.
The following code shows how to use FPDF and FPDI to fill an existing gift coupon with the user data.
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile('gift_coupon.pdf');
// import page 1
$tplIdx = $this->pdf->importPage(1);
//use the imported page and place it at point 0,0; calculate width and height
//automaticallay and ajust the page size to the size of the imported page
$this->pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
// now write some text above the imported page
$this->pdf->SetFont('Arial', '', '13');
$this->pdf->SetTextColor(0,0,0);
//set position in pdf document
$this->pdf->SetXY(20, 20);
//first parameter defines the line height
$this->pdf->Write(0, 'gift code');
//force the browser to download the output
$this->pdf->Output('gift_coupon_generated.pdf', 'D');
If you need really simple PDFs, then Zend or FPDF is fine. However I find them difficult and frustrating to work with. Also, because of the way the API works, there's no good way to separate content from presentation from business logic.
For that reason, I use dompdf, which automatically converts HTML and CSS to PDF documents. You can lay out a template just as you would for an HTML page and use standard HTML syntax. You can even include an external CSS file. The library isn't perfect and very complex markup or css sometimes gets mangled, but I haven't found anything else that works as well.
Don't know if this is an option, but it would work very similar to Zend's pdf library, but you don't need to load a bunch of extra code (the zend framework). It just extends FPDF.
http://www.setasign.de/products/pdf-php-solutions/fpdi/
Here you can basically do the same thing. Load the PDF, write over top of it, and then save to a new PDF. In FPDI you basically insert the PDF as an image so you can put whatever you want over it.
But again, this uses FPDF, so if you don't want to use that, then it won't work.
Zend Framework can load and edit existing PDF files. I think it supports revisions too.
I use it to create docs in a project, and it works great. Never edited one though.
Check out the doc here
The PDF/pdflib extension documentation in PHP is sparse (something that has been noted in bugs.php.net) - I reccommend you use the Zend library.
Tcpdf is also a good liabrary for generating pdf in php
http://www.tcpdf.org/
We use pdflib to create PDF files from our rails apps. It has bindings for PHP, and a ton of other languages.
We use the commmercial version, but they also have a free/open source version which has some limitations.
Unfortunately, this only allows creation of PDF's.
If you want to open and 'edit' existing files, pdflib do provide a product which does this this, but costs a LOT
<?php
//getting new instance
$pdfFile = new_pdf();
PDF_open_file($pdfFile, " ");
//document info
pdf_set_info($pdfFile, "Auther", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Creator", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Title", "PDFlib");
pdf_set_info($pdfFile, "Subject", "Using PDFlib");
//starting our page and define the width and highet of the document
pdf_begin_page($pdfFile, 595, 842);
//check if Arial font is found, or exit
if($font = PDF_findfont($pdfFile, "Arial", "winansi", 1)) {
PDF_setfont($pdfFile, $font, 12);
} else {
echo ("Font Not Found!");
PDF_end_page($pdfFile);
PDF_close($pdfFile);
PDF_delete($pdfFile);
exit();
}
//start writing from the point 50,780
PDF_show_xy($pdfFile, "This Text In Arial Font", 50, 780);
PDF_end_page($pdfFile);
PDF_close($pdfFile);
//store the pdf document in $pdf
$pdf = PDF_get_buffer($pdfFile);
//get the len to tell the browser about it
$pdflen = strlen($pdfFile);
//telling the browser about the pdf document
header("Content-type: application/pdf");
header("Content-length: $pdflen");
header("Content-Disposition: inline; filename=phpMade.pdf");
//output the document
print($pdf);
//delete the object
PDF_delete($pdfFile);
?>

UTF-8 PDF generated with TCPDF showing up fine in Adobe Acrobat but corrupted in Illustrator and Google preview

I use PHP and TCPDF to generate online invoices encoded in utf8. I've created font definition files following the tutorial on the tcpdf website using PT-sans .ttf file.
Generated pdf files look fine (accents are displayed correctly) when opened with Adobe Reader. But as soon as you open the pdf file with Adobe Illustrator, you'll see a mix of normal ascii characters and missing characters signs. When opened as attachment in the gmail preview, no text is displayed at all, just images will show up.
I also get unconsistent behavior with Foxit reader. It was ok on one computer (with font installed), but corrupted on another (without the font installed).
Illustrator (on the left) and Foxit reader printscreen: http://dl.dropbox.com/u/14647415/illustrator%20and%20foxit.jpg
Here is a copy of the corrupted file: http://dl.dropbox.com/u/14647415/2011040-3.pdf
PDF's size is ±200kB, so font seems to be embeded correctly.
This seems to happen with any utf8 font, embeded with procedure described on the tcpdf website. When I tried the core cid fonts, that came with the tcpdf library, accented characters were missing, or have been replaced with ?, but the overal layout and text were ok in Illustrator, too. Attached utf-8 fonts like Dejavusans are showing up corrupted, too.
I use 'UTF-8' as a param passed into tcpdf constructor.
class XTCPDF extends TCPDF
{
function __construct(){
parent::__construct('P', 'mm', 'A4', true, 'UTF-8', false);
...
}
...
}
Do you have any ideas on what could be causing this?
Thank you very much in advance.
p.
Problem was obviously caused by TCPDF automatically subsetting the font. This caused problems with charset (characters were "shifted", '0' interpreted as 'A' etc.) in aplications other than Acrobat.
Solution to this is setting:
$pdf->setFontSubsetting(false);
and subsetting your font manually to reduce the resulting file size, based on charset you want to use.

Categories