PHP PDFlib unicode - php

is it possible to render pdf in UTF-8. for persian or arabic characters? i dont wanna use tcpdf. it's have a performance problem during make pdf via php scripts. I will try latest version of php 5.4 and pdflib 8.0.4p2 but i cant see valid persian words.
<?php
try {
$p = new PDFlib();
/* open new PDF file; insert a file name to create the PDF on disk */
if ($p->begin_document("", "") == 0) {
die("Error: " . $p->get_errmsg());
}
$p->set_info("Creator", "hello.php");
$p->set_info("Author", "Rainer Schaaf");
$p->set_info("Title", "Hello world (PHP)!");
$p->begin_page_ext(595, 842, "");
$font = $p->load_font("Courier", "unicode", "");
$p->setfont($font, 24.0);
$p->set_text_pos(50, 700);
$p->show("Hello world!");
$p->continue_text($p->utf8_to_utf16("(says PHP) سلام جهان", 'utf16'));
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;
}
catch (PDFlibException $e) {
die("PDFlib exception occurred in hello sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
}
catch (Exception $e) {
die($e);
}
$p = 0;

The main problem is that the font Courier does not have glyphs for the arabic text you want to print. If you use a font like Arial Unicode you will see the expected result.
Another tip, use $p->set_parameter("textformat", "utf8");
then you can use utf8 directly as in $p->continue_text("(says PHP) سلام جهان x");

Related

PDFlib PHP place SVG into table cell

I want to create a table where every item gets a hyphen next to it, which is provided as an svg graphic. I tried to load it into a table cell similar to an image, but that does not seem to work. I also couldn't find anything related to that in the PDFlib Cookbok.
I then tried to just place the graphic outside of the table, but now I have the problem, that since it's not connected to the table, the hyphens do not go to a new page, if the table is too large for the current one and loads the rest of the content on that new page. So this does not seem to do the trick either. Does anyone know a solution on how to load the svg graphic inside of the table, so it renders with it's content (meaning they also go on new pages)?
Here's the code where I tried to load the svg into the table:
## add hyphen to table ##
$image = $p->load_graphic("auto", $hyphen, "");
if ($image == 0) {
echo("Couldn't load $image: " . $p->get_errmsg());
exit(1);
}
$optlistHyphen = "colwidth=2% margintop=3 graphic=" . $image;
$tbl = $p->add_table_cell($tbl, $col1, $row, '', $optlistHyphen);
if ($tbl == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
And here's the code of my current try, where I placed the graphic outside of the table:
## add hyphen ##
// load hyphen
$graphics = $p->load_graphics('auto', $hyphen, '');
if ($graphics == 0) {
echo('Couldn not load logo image: ' . $p->get_errmsg());
exit(1);
}
// place hyphen
$buf = "scale=0.25";
$p->fit_graphics($graphics, $hyphenX, $hyphenY, $buf);
// calculate new height for hyphen
$hyphenY = $hyphenY - 16;
// Add new empty table cell
$optlistTableCellHyphen = "colwidth=2%";
$tbl = $p->add_table_cell($tbl, $col1, $row, '', $optlistTableCellHyphen);
if ($tbl == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
(both these code snippets are inside of a foreach for the table content)
Also here's an image of the current ouput with the last "solution":
Also from your explanation it is not clear to me why placing an SVG file in a table cell should not work. It may be important to use the fitgraphics options to define the size and placement of the graphic. (See also PDFlib 9.3.1 API Reference, Chapter 5.3, Table 5.16.).
<?php
try {
$searchpath = "../data";
$p = new PDFlib();
$tbl = 0;
# This means we must not check return values of load_font() etc.
$p->set_option("errorpolicy=exception stringformat=utf8");
/* Set the search path for font files */
$p->set_option("SearchPath={{" . $searchpath . "}}");
/* open new PDF file; insert a file name to create the PDF on disk */
$p->begin_document("", "");
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$graphics = $p->load_graphics('auto', 'PDFlib-logo.svg', '');
for ($i=1; $i < 10; $i++){
$tf = $p->create_textflow('Feature ' . $i . '<nextline>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna ', 'fontname=NotoSerif-Regular encoding=unicode fontsize=12');
$tbl = $p->add_table_cell($tbl, 1, $i, '', 'colwidth=30 graphics=' . $graphics . ' fitgraphics={position={center top}} margin=3');
$tbl = $p->add_table_cell($tbl, 2, $i, '', 'colwidth=250 textflow=' . $tf . ' rowheight=10');
}
$p->fit_table($tbl, 50, 50, 545, 792, "stroke = {line=horother}");
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=sample.pdf");
print $buf;
}
catch (PDFlibException $e) {
die("PDFlib exception occurred in this sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
}
catch (Throwable $e) {
die("PHP exception occurred: " . $e->getMessage() . "\n");
}
$p = 0;
?>
I have put together a very simple example which only uses sample resources from the PDFlib packages. To keep the code shorter, I set errorpolicy=exception.
This code produces this output which should be very close to what you are looking for. (only one page here, but you can probably already see the difference to your implementation)
If you want to place the graphics outside the table, it might be useful to use info_table() to get the Y-positions for the vertical lines after placing the table (see PDFlib 9.3.1 API Reference, Chapter 5.3, Table 5.19).
This allows you to determine exactly where you would have to place the graphics, even if it continues on another page (instance).

mPDF not generating PDF in firefox

I am using mPDF library for creating PDF using html data.it works fine in google chrome,but in firefox it displays nothing.No files downloaded and no errors in console.
Here is my code :
require_once( $destination_path."/mpdf/mpdf.php");
$target_path = $destination_path . '/uploads/reports/';
$htmlData='<span>Sample Html Content</span>';
try {
$pdf= new mPDF();
$pdf->debug = true;
$pdf->SetFooter("MyApp" . '|{PAGENO}|' . date(DATE_RFC822));
$pdf->WriteHTML($htmlData);
$pdf->Output($fileOutputPath, 'F');
}catch (Exception $e) {
echo 'Caught exception: ', $e, "\n";
}
i am tried ob_clean() and headers for solve the issue,but it remains same .
Anyone knows how to solve this issue?

strange symbol in RecursiveIteratorIterator

Hello I have Problem While I Listing Files in folder named upload, When There's arabic file It show ��� ����� ������
$target = "upload";
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($target));
while($it->valid()) {
if (!$it->isDot()) {
$nom=$nom+1;
echo $it->getSubPathName();
echo '<tr align="center"><td> Play </td><td>' . $it->getSubPathName() . '</td><td>' . $nom . '</td></tr>'; } }
First, convert the text to UTF-8:
iconv('CP1256', 'UTF-8', $it->getSubPathName());
Then, ensure that the web browser properly decodes the page as UTF-8. Put this as the top of your PHP file:
<?php
header("Content-Type: text/html; charset=UTF-8");
// The rest of the code

Delete system tmp file with COM and Word

I am using COM to convert dynamically created word docx to PDFs. Everything is working perfectly, but our c:\windows\temp\ directory is getting extremely large (and we have limited disk space on our C drive). Below is the code I am using to generate the PDF. Is there a way to delete the temp file that gets added to the system's temp directory after the file is closed? In addition, after the word doc is converted to pdf, we no longer need it (so it doesn't matter if deleting the tmp file corrupts the word doc).
/Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0; //don't open word gui on server
$word->DisplayAlerts = 0; //don't allow any alerts to open
//open doc
try{
$doc = $word->Documents->Open(DOC_LOCATION . "temp_doc/" . $filename . ".docx");
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "temp_pdf/" . $filename . ".pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Quit();
$word = null;
Don't say what version, but for XP- you can use a simple batch file to delete the files in \Documents and Settings\USER\Local Settings\temp\. Example:
C:
cd \Documents and Settings\USER\Local Settings\Temp
attrib -R -S -H *.* /S /D
DEL /S /F /Q *.*
Modify accordingly.

Word Doc File to PDF on Windows Server

I am trying to convert .docx files to pdf using PHP on a Windows server. I tried several of the solutions from other posts, including phpdocx (which does a very poor conversion that doesn't keep any formatting) and php's Com object. I only have Office 2003, so there is no pdf converter available using Com.
I thought of using OpenOffice/LibreOffice, but haven't found any information about installing and using Com for these on a windows server (I know it can be installed, but I can't figure out how to set up Com for it).
Using a webservice is not an option due to the data on the forms (they have to remain on our server). This means that Zend Framework cannot be used.
Any suggestions would be helpful, or information about using Com with Open Office.
I was finally able to get this working. OUr problem was that Word 2003 didn't have a PDF converter in it. We ended up using a trial version of Office 2010 for now (assuming that everything works correctly, we will purchase the full version). Word 2007 would have worked also. Below is the code I used to get this working:
//Word Doc to PDF using Com
ini_set("com.allow_dcom","true");
try{
$word = new com('word.application') or die('MS Word could not be loaded');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
$word->Visible = 0;
$word->DisplayAlerts = 0;
try{
$doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "doc opened";
try{
$doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
}
catch (com_exception $e)
{
$nl = "<br />";
echo $e->getMessage() . $nl;
echo $e->getCode() . $nl;
echo $e->getTraceAsString();
echo $e->getFile() . " LINE: " . $e->getLine();
$word->Quit();
$word = null;
die;
}
echo "created pdf";
$word->Quit();
$word = null;

Categories