I am starting a new thread for this one, will try to be as specific as possible.
This is a portion of an app for our sales people that allows them to customize a page with their contact information, after they hit submit, a PDF page is created which is then attached to the end of a larger document.
The first page of the document is also dynamically created from the same form that they fill out. After they press submit, 2 PDF files are created, a front page and a back page, then both pages are attached to a larger document creating a single report.
Everything works great unless they enter Chinese characters then all I get is garbage characters like this - (质釕俕试æμ•ç¨‹). The document still looks like this even before it is merged with the larger document.
I have tried every possible scenario (it seems) from using a Unicode font, to using my own created font and one by one trying each of the fonts in the TCPDF folder. I am at a total loss, and have been for some time now. I feel like I have been on just about every website that mentions TCPDF.
Here is the code that creates the back page, back to the basic commands I started with. I am hoping it is a simple command down here that I am missing:
require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB));
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($html, isset($_GET['vuehtml']));
//Line below saves PDF to file
$html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
echo $e;
}
And for what it is worth, running a Centos 5.5 server to create the documents. Thank you in advance.
After doing some investigation on this issue with HTML2PDF, I was told that the font arialuni.ttf should support all of the characters I need. With that being said, HTML2PDF will not load this font when I use it in my script seen below.
require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L','LETTER','en', false, '', array(mL, mT, mR, mB));
$html2pdf->pdf->AddTTFFont('ARIALUNI.TTF');
$html2pdf->pdf->setFont('arialuni');
$html2pdf->writeHTML($html, isset($_GET['vuehtml']));
//Line below sends output to the screen only
//$html2pdf->Output('exemple00.pdf');
//Line below saves PDF to file
$html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
echo $e;
}
*All I can think is that the syntax is not 100% correct. I have ran the tt2fm and makefont to get the files but do not feel that those routines were a 100% successful.
Please help - I have been working on this thing off and on for months. I am almost ready to just scrap it and hit the door.
Thank you.
According to your codes:
require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB));
//$html2pdf->setDefaultFont('Arial'); //Please remove this
$html2pdf->setDefaultFont('stsongstdlight'); //And change with this, it works 101% for me
$html2pdf->writeHTML($html, isset($_GET['vuehtml']));
//Line below saves PDF to file
$html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
echo $e;
}
...And here is my 101% working codes to have the CHINESE characters in pdf output:
<?php
require_once ROOT_DIRECTORY.'/{it's your own location where you put the class at}/html2pdf-4.4.0/html2pdf.class.php'; //ROOT DIRECTORY is my own "defined" so do not use this line.
/* or use this:
* require_once(dirname(__FILE__).'/html2pdf.class.php');
* it depends on what you need
*/
try
{
$html2pdf = new HTML2PDF('L', 'A6', 'en', true, 'UTF-8', 0); //I'm using UTF-8 //Also I'm working on A6 page in my project here, so if you need to have A4 size change A6 back to A4 or change to the size you need
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->setDefaultFont('stsongstdlight'); //this is all you need to have the CHINESE character.
$content = ob_get_clean();
$html2pdf->writeHTML($content);
$html2pdf->Output('Sticker-'.$this->invNumber.'-'.$this->custCode.'.pdf');
exit;
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
Hopefully it works TOO in your codes there!
Happy coding :)
You can render Chinese character in your PDF document by using
cid0jp font in TCPDF.
For your reference :-
PHP Code :-
http://www.tcpdf.org/examples/example_038.phps
Generated PDF :-
http://www.tcpdf.org/examples/example_038.pdf
Please ensure file cid0jp.php should be in font directory.
tcpdf/fonts/cid0jp.php
$html2pdf = new HTML2PDF('L','LETTER','en', true, 'UTF-8', array(mL, mT, mR, mB));
So html2pdf is based on tcpdf, but they are a little bit different:
html2pdf does not support Chinese language.
However, you can get the javiergb.php from the include directory /tcpdf/fonts/. Then you copy this file to the html2pdf/_tcpdf5.xxxx/fonts/ directory.
Afterwards, you just have to add the line $html2pdf->setDefaultFont('javiergb'); to your html2pdf set up as following:
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->setDefaultFont('javiergb');
$html2pdf->writeHTML($html);
$html2pdf->Output('example_zh_cn.pdf');
You can look my fork: https://github.com/cychai/html2pdf
Related
I have downloaded PHP HTMLtoPDF converter from here. But when tried to just print single line output, it does not print in the output pdf. Its empty.
Below is the code which i tried.
$content1 = "<page><a>Sample PDF file</a></page>";
// convert in PDF
require_once('/home/www/APIs/html2pdf_v4.03/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->setModeDebug();
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content1);
$html2pdf->Output('exemple00.pdf');
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
Whenever I get into trouble with something I simplify. Exterminate all the possibilities that could hinder the workings of a program.
With this in mind I have looked at the website and, probably just like you, found a sample piece.
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML("text");
$html2pdf->Output('exemple.pdf');
Try this and see if it works. Why? This is the simplest of setups. If this does not work you know it's not you (or your refrence to the class is wrong).
What I know from the pdf to html creators is that they all have a problem converting incorrect html to a pdf. In this case your "a" could be interpreted as wrong seeing it has no href (which could be required).
So to make this simple use a single line of string text without html like "test" and check back with us. But if that doesn't work I's probably suggest using another of the converters (which I would recommend anyway).
BTW, I use https://github.com/dompdf/dompdf which is very advanced in html error detection and common options.
I am using HTML2PDF converter in PHP to generate PDF.
My html seems to be big so that it could not fit in single page.
I am getting following error when I tried to generate pdf..
'the contents of a TD tag does not fit on a single page'
could not find proper solution yet. Any idea please.
This is my code at end to generate pdf.
require_once("../html2pdf_new/html2pdf.class.php");
try
{
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
$content contains all html.
You can set setTestTdInOnePage as false to overcome this error.
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->setTestTdInOnePage(false);
If this problem still exists: I fixed it with the method
HTML2PDF::setTestTdInOnePage(false)
This setting of value works. In the html2pdf main class file we have a method setTestTdInOnePage which does the rendering of td within a single page if set to true. As it is always true we have this exception thrown as our td data exceeds more than one page. So to avoid it we can set it to false to pass the check.
if we have nested tables, it breaks for multiple pages.
Try to avoid nested tables.
It works.
found at Forum
Find this 'throw new HTML2PDF_exception(7);' in html2pdf.class.php and disable it.
Like this:
if ($this->_testTdInOnepage && $this->_subHtml->pdf->getPage()>1) {
//throw new HTML2PDF_exception(7);
}
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);
?>
I'm currently writing a code to output a pdf file in PHP using PDFlib from http://www.pdflib.com/. The problem is all html tag is also written in the output file. How can be able to cancel out all those tags?
Here is my sample code.
$postVariable = $_POST;
$contentData = "";
foreach($postVariable as $key => $value){
if(is_array($key)){
foreach($key as $key1 => $value1){
$contentData.= $key1 .": ". $value1."<nextline>";
}
}else{
$contentData.= $key .": ". $value."<nextline>";
}
}
$testdata = nl2br($contentData);
$pdf = pdf_new();
// open a file
pdf_open_file($pdf, $_SERVER['DOCUMENT_ROOT']."cas".DIRECTORY_SEPARATOR."$filename.pdf");
// start a new page (A4)
pdf_begin_page($pdf, 595, 842);
// Locate Font Directory
$fontdir = "C:\WINDOWS\Fonts";
// Font Name Parameters
pdf_set_parameter($pdf, "FontOutline", "arialMyName=$fontdir\arial.ttf");
// Find Font
$arial = PDF_findfont($pdf,"arialMyName","host",0 );
// Set font size and font name
pdf_setfont($pdf, $arial, 10);
//$arial = pdf_findfont($pdf, "Arial", "host", 1);
//pdf_setfont($pdf, $arial, 10);
// print text
pdf_show_xy($pdf, "TO THE UNIT OWNER",50, 750);
pdf_show_xy($pdf, "Test ext", 50,730);
pdf_show_xy($pdf, "test test", 50,715);
pdf_show_xy($pdf, $contentData, 50,700);
// end page
pdf_end_page($pdf);
// close and save file
pdf_close($pdf);
and the sample output is:
TO THE UNIT OWNER
Test text
test test
type: Apartment****var_name: ****var_company: ****var_date: ****submit: Save and Download<
It disregards the html tags and it include it on the content.
Is there any other methods on how to print out HTML to PDF using the library that I'm currently using (PDFlib).
Thanks.
regards,
Resty
I would recommend the TCPDF library. It can convert your HTML to a PDF file, including CSS code. It has support for quite a lot of HTML tags, and does a decent job. HOWEVER, from personal experience, while it is very possible to generate high quality PDF files with it, the results are not always 100% as expected. It might need a bit of tweaking and fiddling to get the result you want.
If you only want to remove the HTML tags, I think you might want to take a look at strip_tags. But I guess that's not really what you're after.
Finally, there's a really really cool new kid on the block - a PHP extension that uses the WebKit HTML and rendering engine to generate PDFs. It is called the libwkhtmltox extension and can be found here. It produces amazing results. For example, look at this PDF it made from the homepage of the New York Times - see (http://www.2shared.com/document/kYuS_G7p/nytimes.html - click "save to my pc" down below). That output was generated without specifying any additional options. Exceptional. HOWEVER, you need to get it running as PHP extension, and that might be a non-trivial task. So I would say: stick with the TCPDF library for now.
You cannot generate PDF content from HTML by using PDFLib. Ok, you can write convertor, but that's a huge work to do.
I recommend using TCPDF which supports HTML input.
Man... I was trying to do same thing once. It is very hard to work with pdflib directly.
I found this project, hosted on Google code site. The project deserved 10 stars out of 5.
It is called DOM PDF, you can download it here. You can generate PDFs from HTML pages, with CSS support. You can generate PDF with less than 10 lines of code!
Look at samples to see it in action.
In a web app developed in PHP we are generating Quotations and Invoices (which are very simple and of single page) using TCPDF lib.
The lib is working just great but it seems to generate very large PDF files. For example in our case it is generating PDF files as large as 4 MB (+/- a few KB).
How to reduce this bloating of PDF files generated by TCPDF?
Here is code snippet that I am using
ob_start();
include('quote_view_bag_pdf.php'); //This file is valid HTML file with PHP code to insert data from DB
$quote = ob_get_contents(); //Capture the content of 'quote_view_bag_pdf.php' file and store in variable
ob_end_clean();
//Code to generate PDF file for this Quote
//This line is to fix a few errors in tcpdf
$k_path_url='';
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF();
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// add a page
$pdf->AddPage();
// print html formated text
$pdf->writeHtml($quote, true, 0, true, 0); //Insert Variables contents here.
//Build Out File Name
$pdf_out_file = "pdf/Quote_".$_POST['quote_id']."_.pdf";
//Close and output PDF document
$pdf->Output($pdf_out_file, 'F');
$pdf->Output($pdf_out_file, 'I');
///////////////
enter code here
Hope this code fragment will give some idea?
You need to see what it is putting inside the PDF. Is it embedding lots of images or fonts?
You can examine the contents with lots of PDFtools. If you have Acrobat 9.0, there is a blog article showing how to do this at http://pdf.jpedal.org/java-pdf-blog/bid/10479/Viewing-PDF-objects
Finally I have managed to solve the problem.
The problem was that by mistake I had inserted a link to email id in the web page that was getting rendered to PDF. By just removing this link the size of the generated PDF went down to just 260 kb!
Thanks everyone who tried to help me out in solving this problem.
Current TCPDF version now includes font subsetting by default to dramatically reduce PDF size.
Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.