how generate PDF document with MYSQL query more than 15 fields with wrap text? I have generated PDF with fpdf option. but I have created only 8 fields with nowrap text. so any one please solve the solutions. thanks.
For example. I have created with nowrap text.
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->SetTextColor(4,126,167);
$pdf->Cell(10,10,'Worksheet Report');
$pdf->Ln();
//Column titles
$header=array('Client','Team In Charge','Staff In Charge','Priority','Master Activity','Sub Activity','Last Reports Sent','Job in Hand','Team Incharge Notes','External Due Date','Befree Due Date','Status');
$pdf->SetFont('Arial','B',6);
$pdf->FancyTable($header);
$pdf->SetFont('Arial','',5);
$pdf->SetFillColor(255,255,255);
$pdf->SetTextColor(0);
$pdf->SetLineWidth(.2);
$sql=$_SESSION['query'];
$result = mysql_query($sql);
while($row = #mysql_fetch_array($result)){
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_CompanyName"]),1,0,'T',true);
$pdf->Cell(17,10,$commonUses->getFirstLastName($row["wrk_TeamInCharge"]),1,0,'T',true);
$pdf->Cell(17,10,$commonUses->getFirstLastName($row["wrk_StaffInCharge"]),1,0,'T',true);
$pdf->Cell(12,10,htmlspecialchars($row["lp_wrk_priority"]),1,0,'T',true);
$pdf->Cell(17,10,htmlspecialchars($row["lp_wrk_MasCode"]).($row["lp_wrk_MasCode"]!=""? "-":"").htmlspecialchars($row["lp_wrk_MasterActivity"]),1,0,'T',true);
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_SubCode"]).($row["lp_wrk_SubCode"]!=""? "-":"").htmlspecialchars($row["lp_wrk_SubActivity"]),1,0,'T',true);
$pdf->Cell(19,10,htmlspecialchars($row["wrk_Details"]),1,0,'T',true);
$pdf->Cell(18,10,htmlspecialchars($row["wrk_Notes"]),1,0,'T',true);
$pdf->Cell(19,10,htmlspecialchars($row["wrk_TeamInChargeNotes"]),1,0,'T',true);
$pdf->Cell(18,10,$commonUses->showGridDateFormat($row["wrk_DueDate"]),1,0,'T',true);
$pdf->Cell(18,10,$commonUses->showGridDateFormat($row["wrk_InternalDueDate"]),1,0,'T',true);
$pdf->Cell(15,10,htmlspecialchars($row["lp_wrk_Status"]),1,0,'T',true);
$pdf->Ln();
}
There's a webservice called DocRaptor that uses Prince to generate pdf and xls from html. They offer a free plan as well as larger paid plans.
check this web link :-
http://qualitypoint.blogspot.com/2010/07/resolved-wrap-text-issue-in-fpdf-table.html
http://blog.themeforest.net/tutorials/how-to-create-pdf-files-with-php/
I hope it is useful for you :)
Have a look at the dompdf class. It generates the pdf from your html code so it's much easier and quicker to get the results you want. It's still a bit finicky with floats so you have to imagine your are in the 90's and do a table based design.
My advice is to print out the table in html, than convert in into pdf: is easier, faster, and more flexible: when you have the html documents, will be easy (for example) convert into a jpg, if you'll need to in the future.
If you can install software on your server, i raccomend you wkhtmltopdf.
You generate the html file, and wkhtmltopdf convert it to pdf, with full (webkit based) css support.
Else, i've found that html2ps doeas a great job, but is quite slower with large files.. css support is good, but no guarantee that it will support css3, for example.
Related
I am trying to convert a DOCX file to PDF with PHPWord. When I execute the script it looks like that some style elements are not converted. In the DOCX file I have one image, two tables with border 1px and hidden borders and I am using Tabs.
When I execute the script I get a PDF file without the image, all the Tabs are replaced with Space and all the tables have a border 3px.
Does someone know why I am missing these styles?
Here is my script:
while ($data2 = mysql_fetch_array($rsSql)){
$countLines=$countLines+1;
$templateProcessor->setValue('quantity#'.$countLines, $data2['quantity']);
$templateProcessor->setValue('name#'.$countLines, $data2['name']);
$templateProcessor->setValue('price#'.$countLines, "€ " .$data2['price'] ."");
}
\PhpOffice\PhpWord\Settings::setPdfRenderer('./dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath('./dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DOMPDF');
$temp_file = tempnam(sys_get_temp_dir(), 'Word');
\$templateProcessor->saveAS($temp_file);
$phpWord = \PhpOffice\PhpWord\IOFactory::load($temp_file);
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='result.pdf'");
readfile("result.pdf");
After a look on the source code, it seems that PHPWord previously converts the document into an HTML representation before letting it be saved it into PDF by dompdf, another converter.
That's what the opened issue #1139 confirms, moreover it deals with styles missing:
The PDF writers being used are taking in the HTML output, which also lacks the styling. The classes are being defined in the <style> tag, but they are just not being used.
Also the last message adds:
This still seems to be an issue. html and pdf outputs do not replicate the some styles in docx (header / footers).
Concerning your border problem, another SO question shows a similar issue in a conversion HTML -> PDF. A solution was to edit the CSS style, which you obviously cannot perform in your sample code, unless you proceed to pre-convert into HTML.
In conclusion, you may not solve your problem in the short term. If you won't be a part of the dev team, you could submit bug reports to them (and not to dompdf, since it's an HTML-to-PDF converter and they are outside the scope). Github lets you to add DOCX files to the issue report.
Alternatives
You could check out a SO question 204860 about server sides PDF editing library. Below two alternatives, one is free software, the other is closed source and priced.
LibreOffice
Another way is to use LibreOffice in headless mode (command line execution without interface):
libreoffice --headless --convert-to pdf <filename_to_convert>
A PHP wrapper for LibreOffice, Office Converter is also available here if you don't want to bother using libreoffice through exec().
Check if LibreOffice conversion will suit your needs (it may not cover all cases, but be satisfying your scope).
Aspose
The best converter I ever used at work is Aspose, an API covering Documents with Aspose.Words package, Worksheets with Aspose.Cells, Presentations with Aspose.Slides and so on. But it's closed-source and pretty expensive (and you'll pay for updates if you want them after your license expiration).
There is a way to use it in PHP through Java (Aspose.Words and Aspose.Cells) or .NET (Aspose.Words same seems to go with Aspose.Cells).
I have been looking and testing this for a couple days now and was wondering if anyone could point me in a different direction. I have a very long job application HTML form (jobapp.html) and a matching PDF (jobpdf.pdf) that have the same field names for all entries in both the HTML form and the PDF. I need to take the user data that is entered in the form and convert it to a PDF. This is what I have gathered so far but don't know if I am on track:
Is pdftk the only viable 3rd party app to accomplish this?
Using pdftk would i take the $_POST data collected for the user and generate a .fdf(user.fdf) then flatten the .fdf on the .pdf(job.pdf). So irreguarless of where the fields are located on each document the information on the fdf would populate the pdf by field names?
I have been trying
http://koivi.com/fill-pdf-form-fields/tutorial.php
I have also looked at "Submit HTML form to PDF"
I have used fpdf several times to create php-based pdf documents. An example following:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddFont('georgia', '', 'georgia.php');
$pdf->AddFont('georgia', 'B', 'georgiab.php');
$pdf->AddFont('georgia', 'I', 'georgiai.php');
# Add UTF-8 support (only add a Unicode font)
$pdf->AddFont('freesans', '', 'freesans.php', true);
$pdf->SetFont('freesans', '', 12);
$pdf->SetTitle('My title');
$pdf->SetAuthor('My author');
$pdf->SetDisplayMode('fullpage', 'single');
$pdf->SetLeftMargin(20);
$pdf->SetRightMargin(20);
$pdf->AddPage();
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
You can learn very fast with these tutorials from the website itself.
EDIT: Example to save form data: (yes, is very easy...)
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
foreach ($_POST as $key =>$data)
{
$pdf->Write(5, "$key: $data"); //write
$pdf->Ln(10); // new line
}
$pdf->Output($path_to_file . 'file.txt','F'); // save to file
Look at these pages created with fpdf, really!
http://www.fpdf.org/
That would be the library to do it. I used it here to add images to a form and submit it to create a PDF with those images: http://productionlocations.com/locations
The actual code to do it is pretty complex.
I have found PrinceXML very easy to use. It takes your HTML/XML, applies CSS, and converts it into a PDF. The PHP extensions work very well. Unfortunately, it's not free.
One way you can consider is using an online API that converts any HTML to PDF. You can send them a generated HTML (easier to produce) that will contains your user's submitted data, and receive back a high fidelity PDF.
There are quite a few services available on the market. I like to mention PDFShift because it offers a package in PHP that simplifies the work for you.
Once you've installed it (using Composer, or downloaded it directly, depending on your choices) you can quickly convert an HTML document like this:
require_once('vendor/autoload.php');
use \PDFShift\PDFShift;
PDFShift::setApiKey('{your api key}');
PDFShift::convertTo('https://link/to/your/html', null, 'invoice.pdf');
And that's it. There are quite a few features you can implement (accessing secured documents, adding a watermark, and more).
Hope that helps!
I want to modify a PDF document with PHP. I found the libs FPDF and FPDI which allows to create and modify PDF files. Here is my very simple code:
<?php
require_once('include/fpdf.php');
require_once('include/fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('input.pdf');
// import page 1
$page1 = $pdf->importPage(1);
// insert the page
$pdf->useTemplate($page1);
// now write some text above the imported page
$pdf->AddFont('calibri');
$pdf->SetFont('calibri','',11);
$pdf->Write(0, "This is just a simple text");
header("Content-Type: application/pdf");
$pdf->Output();
That works very well, but I see that the inserted text adds something like a reference to the system font instead of the already embedded font. How can I simply reuse the embedded font?
If that is not possible with that libs feel free to point me to a third free lib.
I know that I can just use characters which are already used, but this is no problem in my special case. I have checked that all possible characters are used in the right case sensitivity.
I bet your embedded font from the imported PDF page is not called calibri. (Lower-case only spelling of fontnames in PDFs is very rare.)
And I also bet, that the PDF will not have Calibri embedded as a full set. It's rather likely that it is a subset only. And as a subset, the fontname will be composed of a random 6 letter uppercase prefix + the original font name, like this:
AXBTZV+Calibri
You have to find that exact name and try with this. (However, I'm not sure if how your PHP library works, if it can do that at all, and if it would handle the modification of the PDF text writing code in the correct way. Gimme the PDF and I most likely can do it in a Text Editor, though...)
Reusing fonts in PDFs is mostly not possible since only the characters that are used in the PDF are stored. So if you never used an uppercase W in your PDF template and want to add text with one, then it cannot be displayed.
This does not answer your question but rather tells you not to try it at all.
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.
I am using php code to query to a database and the results will be used to generate a report.
If I want the report to be generated in a pdf format how should I do it ?
If you need UTF support in your PDF file, consider tcpdf library.
Download it from here: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
And in your script:
<?php
//include files
require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/config/lang/eng.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//add some content using class methods
//Close and output PDF document
$pdf->Output('filename.pdf', 'I');
?>
Look into html2pdf
Create your report as html and then run the code to transform into PDF. You don't need to know the language to generate the PDF blocks. Submitted forms work out cool too.
You can also use FPDF, I've used that for several projects. In the beginning you will be annoyed a lot, but when you get used to it, it will get easier to create pdf's :)
I've used TCPDF (http://www.tcpdf.org/) for my last project. It worked pretty good but the next time im going for a html to pdf converter, simply because designing the report (converting to pdf draw statements) was such a time sink.
So i would suggest http://sourceforge.net/projects/html2fpdf/