Special symbols in PDF - php

Im developing a PDF generation script with PHP, using FPDF library, its all fine for text and images, but when I put corrency symbols like Pound or Euro, it is giving some special symbols instead, I could solver the same in normal pages by setting character encoding of the webpage, but not sure how to set character encoding for a PDF document.

You are running into encoding - decoding issues.. Add this line on top of your script.
<?php
header('Content-Type: text/html;charset=utf-8');
followed by your FDPF generation code.

Related

Unable to save file with Thai characters filename

I'm currently trying to save a pdf file using mPDF library. My problem is when I try to output file using English filename, the filename is displayed correctly, but if the filename contains any Thai characters it became weird.
My mPDF outputs code.
$save_file = $s_code.'_'.$classroom.'.pdf';
$mpdf->Output('../../../upload/'.$save_file,'F');
With English filename it displayed correctly.
t10024_201.pdf
With Thai characters it doesn't.
เธ—เธช10024_201.pdf
I can't figure out what causes the problem.
The filename is restricted to the character set supported by the device (in this case, the server where mpdf is generating your pdfs), and doesn't actually reflect a problem with mpdf itself. [If you can add/write Thai characters within the pdf, just the filename doesn't reflect Thai characters].
You may need to configure the Content-Disposition headers for the webserver's response with PDF file. As an example, see this blog post that describes how a ColdFusion application developer dealt with saving files with French characters.
Thank you so much, Anson W Han.
It's about character encoding. I finally found a solution. I simply convert the filename to Thai edcoding using "iconv" and it displays correctly.
The code:
$mpdf->Output('../../../upload/'.iconv("UTF-8", "TIS-620",$save_file),'F');

Missing characters in filled pdf using PDFTk with encoding UTF-8

I'm trying to fill pdf documents using PDFTk. Script working fine, it fills inputs in form but I don't get special characters [polish charset: UTF-8 or ISO-8859-2].
Script: https://github.com/mikehaertl/php-pdftk
The weird thing is that generated pdf actually has polish characters when I click on field.
Before click:
After click on field:
Default encoding is set to UTF-8. The problem is that PDFTk can't use chars outside the standard ASCII with FDF form fill. It doesn't allow multi-byte characters.
What I did:
Add fonts to pdf files (checked and files has font)
Create fields in pdf files with default font (Arial)
Change encoding in script (function fillForm) to ISO-8859-2
Change data values encoding (iconv or mb_convert_encoding)
Change functions encoding and data value encoding to ISO-8859-2
Flatten pdf after filling the form
Read all topics about this problem in stackoverflow, google
UPDATE (25.03.2016): Findout that pdf documents works fine on some computers. Some people have polish characters and other don't. All of
us have right fonts (with polish charset). I used default Arial or
Times New Roman. Fonts are also embed in that file.
Any ideas?
you need to run pdftk with need_appearances as an argument.
kudos to the guys from this issue on github.
I had similar issue.
Solved it with utf8_decode function. eg utf8_decode('Łukasz')
The best results (without flatten) I got when I was creating FDF file with UTF-8 values encoded into UTF-18BE
chr(0xfe) . chr(0xff) . str_replace(array('\\', '(', ')'), array('\\\\', '\(', '\)'), mb_convert_encoding($string, 'UTF-16BE'));
Your library works quite well but ie. when I open the PDF generated with it directly in Safari on MACOS it does not show polish chars until I click the field. When I open it with Adobe Reader - it works fine.
I could not find how to change font, so my solution - use itext, https://itextpdf.com/en/resources/examples/itext-5/filling-out-forms
wrote for my project https://github.com/dddeeemmmooonnn/pdf_form_filler

Issues with Chinese characters in FPDF

I'm generating some order confirmations using FPDF library, which works great. But now I need some Chinese characters in the document. I have searching for many hours by now and have found no workable solution. Is it possible supporting Chinese characters using FPDF or is there an alternative to FPDF which will support it?
All the characters are mixed with regular text and are stored in a MySQL database. The PDF document only show unicode when printing the Chinese characters.
There exist Chinese encoding formats like Big5 or GBK. However, more likely than not, the text you are trying to input is in Unicode. There exists tFPDF, which provides Unicode support. I will test printing the following traditional hanzi: 中國. This means China, for those reading who do not know.
// Remember to copy msjh.ttf to [path to tFPDF]/font/unifont/ directory
// Initialize tFPDF
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font like MSJH
$pdf->AddFont('MSJH','','msjh.ttf',true);
$pdf->SetFont('MSJH','',42);
// Output Chinese string to PDF
$pdf->Text(12,42,"中國");
// Output PDF document
$pdf->Output();
Another alternative is the Code200365k TTF
$pdf->AddFont('Code200365k','','Code200365k.ttf',true);
I use it for Chinese characters only as follows:
if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $my_value)){
$pdf->SetFont('Code200365k','',12);
}
$pdf->Cell(130,9,$my_value,1,1,'L');
$pdf->SetFont('Arial','',11);

XML file isn't UTF-8 encoded when created in PHP

I'm trying to output XML file using PHP, and everything is right except that the file that is created isn't UTF-8 encoded, it's ANSI. (I see that when I open the file an do the Save as...).
I was using
$dom = new DOMDocument('1.0', 'UTF-8');
but I figured out that non-english characters don't appear on the output.
I was searching for solution and I tryed first adding
header("Content-Type: application/xml; charset=utf-8");
at the beginning of the php script but it say's:
Extra content at the end of the document
Below is a rendering of the page up to the first error.
I've tryed some other suggestions like not to include 'UTF-8' when creating the document but to write it separately:
$doc->encoding = 'UTF-8'; , but the result was the same.
I used
$doc->save("filename.xml");
to save the file, and I've tryed to change it to
$doc->saveXML();
but the non-english characters didn't appear.
Any ideas?
ANSI is not a real encoding. It's a word that basically means "whatever encoding my Windows computer is configured to use". Getting ANSI is a clear sign of relying on default encoding somewhere.
In order to generate valid UTF-8 output, you have to feed all XML functions with proper UTF-8 input. The most straightforward way to do it is to save your PHP source code as UTF-8 and then just type some non-English letters. If you are reading data from external sources (such as a database) you need to ensure that the complete toolchain makes proper use of encodings.
Whatever, using "Save as" in an undisclosed piece of software is not a reliable way to determine the file encoding.

Producing symbols from HTML characters in FPDF

I have a government client that requires the legal 'section symbol' (§) in their documents. When creating the documents in a web page, this symbol is created with § or §.
I can not figure out how to get either of these to work in a pdf document created with FPDF. I have tried the iconv and utf8_decode methods, but neither have worked. Any ideas?
You're looking for html_entity_decode().
There is an easier approach to do this:
You can generate a section symbol using FPDF with using ascii code: chr(167).
It's good practice to make this a constant. At the top of your script, add:
define('SECTION',chr(167));
Now you can use SECTION in your script to print the euro symbol.
For example:
echo SECTION.'1.1';
Will output: §1.1
Note: This will also work for other symbols with chr(ascii), where ascii is the ascii code of the symbol. You can find an overview of ascii codes on this page: http://www.atwebresults.com/ascii-codes.php?type=2

Categories