having some problems rendering html when I convert to PDF. It's driving me mental and I don't know what's going on. The idea is that I have an html form in a mysql database which stores HTML tags and what not, I pull it out using PHP, render the HTML and display it as a PDF. My issue is that it won't render the HTML. it's just text and the formatting is way off. Here is the code I have so far:
$indemResult = mysqli_query($conn,"SELECT * FROM Indemnity");
$indemRow = mysqli_fetch_array($indemResult);
require('../include/PDFConverter/fpdf.php');
$pdf = new PDF();
$pdf->SetMargins(0,0,0);
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
//Insert Banner
$pdf->Image('../assets/pdfBanner.png');
$pdf->Ln();
$pdf->Ln();
//Insert Indemnity form
$pdf->SetXY(50, 65);
$pdf->cMargin = 10;
$pdf->SetFont('Arial','',24);
$pdf->Cell(0, 10, $pdf->Write(1,'Indemnity Form'), 0, 1,'C', false);
$pdf->SetFont('Arial','',12);
$text=$pdf->WriteHTML(utf8_decode($indemRow['form']));
$wrap=$pdf->WordWrap($text,120);
$pdf->MultiCell(0,0,$pdf->Write($wrap, ''));
My problem was in the WriteHTML class. It didn't support UL LI tags, so I used this instead:
http://fpdf.de/downloads/addons/53/
I stripped the createPDF class, in the class PDF extends FPDF I removed $_title, $_url, $_debug=false from the PDF function, I also removed $bi from the write HTML function and inside the writeHTML function I removed:
$this->bi=$bi;
if ($bi)
$html=strip_tags($html, "<a><img><p>
<font><tr><blockquote><h1><h2><h3><h4><pre><red><blue><ul><li><hr><b><i><u><strong><em>");
I hope this helps people who are trying to render HTML before putting it onto PDF
Related
May you please help is my first time doing PDF. I want to create a key tags that will fill in the whole PDF so that I can be able to cut them when i print it. I am failing to place a border outside the text to make a square or rectangle. The first three text must be bold the last test not bold.
I want to create something like this:
<?php
$root =realpath($_SERVER['DOCUMENT_ROOT']);
require($root.'/#services/fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Line(10,10,45,10);
$pdf->Line(10,10,10,25);
$pdf->Line(10,10,45,10);
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"Block E",0,'C');
$pdf->Ln(3);
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"Block / Unit - Area",0,'C');
$pdf->Ln(3);
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"Floor",0,'C');
$pdf->Ln(3);
$pdf->SetFont('Arial','',8);
$pdf->Write(0.1,"Door No. Door No",0,'C');
$pdf->Output();
?>
I Want to edit PDF in php using FPDI/FPDF.I want to replace particular text.I have try many solution but they are not giving the desired result.All are writing some new text in new position. I want to search some text and replace that text with a new text.Is This Possible?If yes please explain.Code:
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->AddPage();
//Set the source PDF file
$pagecount = $pdf->setSourceFile("test.pdf");
//Import the first page of the file
$tpl = $pdf->importPage(1);
//Use this page as template
$pdf->useTemplate($tpl);
//Go vertical position
$pdf->SetY(15);
//Select Arial italic 8
$pdf->SetFont('Arial','I',8);
//Print centered cell with a text in it
$pdf->Cell(0, 10, "Hello World", 0, 0, 'C');
//want something like this
$pdf->replace("old_text","new_text");
$pdf->Output("my_modified_pdf.pdf", "F");
It is not possible to edit a PDF document with FPDI and also it is not possible to replace text of an imported PDF page with FPDI.
I am trying to draw line/image on every page of pdf using dompdf but it starts from second page, why this is so ? anyone has any idea ?
Here is my code
$dompdf = new DOMPDF();
$dompdf->load_html($message2);
$dompdf->set_paper('a4','portrait');
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For Header
$header = $canvas->open_object();
$canvas->image($header_image1,'jpg',0, 0, 595, 100);
$canvas->line(0,100,595,100,array(0,0,0),1);
$canvas->close_object();
$canvas->add_object($header, "all");
//For Footer
$footer = $canvas->open_object();
$canvas->line(0,740,650,740,array(0,0,0),1);
$canvas->image($footer_image1,'jpg',0, 742, 595, 100);
$canvas->close_object();
$canvas->add_object($footer, "all");
$output = $dompdf->output();
this code draw line/image on pdf but it only display on last page.
I have two pages in pdf and my line/images are drawn on second page not on first page.
Please suggest any solution.
Adding objects in DomPDF works from the current page onwards. In other words, your objects will get added, but only from the page you currently have and then onwards to any new pages you add.
In your code, you've already converted your HTML to PDF, so the current page is more than likely the last page in your document. So your header / footer are added there, but not to any previous pages.
To place content on every page, domPDF provides two methods: page_text and page_script.
In your case the following type of code should do the trick:
$canvas->page_script('
$pdf->line(10,730,800,730,array(0,0,0),1);
');
Code in the page_script function is then executed for every PDF page.
The line is not displayed for me because embedded php was disabled.
This solved the problem:
$dompdf->set_option("isPhpEnabled", true);
This line has to be placed before $dompdf->render().
how to add image in all pdf page using header and footer.below is my code.
$pdf = App::make('dompdf');
$pdf->loadFile('invoice.html');
$pdf->output();
$dom_pdf = $pdf->getDomPDF();
$canvas = $dom_pdf ->get_canvas();
$image1="logo.png";
$canvas->image($image1,'png', 0, 0, 50, 25);
$canvas->page_text(10, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, array(0, 0, 0));
$pdf->save('pdf_report/eft_payment-'.$RandomAccountNumber.'.pdf');
How to export MySQL database table contents on to a PDF file. It has to be displayed in the website as download/print data. When the customer click on that he/she has to get the PDF opened or an option to download the PDF with all the contents of the table using php.
<?php
require('fpdf17/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
include ('db.php');
$sql = "SELECT studid,name,address,class,phone,email FROM studinfo";
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$studid = $rows[0];
$name = $rows[1];
$address = $rows[2];
$class = $rows[3];
$phone = $rows[4];
$email = $rows[5];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
?>
You should make use of PDF creator libraries like:
FPDF
TCPDF
EzPDF
All three are easy to learn in the order I've put. The documentation of FPDF and EzPDF are very neat and clean. But TCPDF Documentation is not that readable.
Take a look at http://www.tcpdf.org/. I never heard about out of box way to print mysql table data into PDF, but with TCPDF you can programmatically build a PDF file with table filled with data inside it. It also allows to output document created on the fly easily into a browser.
You can use http://www.tcpdf.org/ to create your own PDF. The examples and the doc tabs will help you ^^ (http://www.tcpdf.org/doc/code/classTCPDF.html for all the methods)
You can create a HTML table will all your data and put it in your PDF using the WriteHTML() method.
I am trying to generate a PDF that outputs Item Names onto a template PDF (using FPDI) with the Username listed on the top of each page. Every user can have a different number of items (i.e. if there are 1-4 items, only output one page; if there 5-8 items, output two pages, etc.)
Here is an example of what I am trying to do: http://www.mediafire.com/?k2ngmqm1jmm
This is what I have so far. I am able to get all of the spacing to work by setting a TopMargin, but this doesn't allow me to put the Username header in.
<?php
require_once('auth.php');
require_once('config.php');
require_once('connect.php');
$username=$_GET['username'];
$sql="SELECT * FROM $tbl_items WHERE username='$username'";
$result=mysql_query($sql);
require_once('pdf/fpdf.php');
require_once('pdf/fpdi.php');
$pdf =& new FPDI();
$pdf->SetTopMargin(30);
$pdf->AddPage('L', 'Letter');
$pdf->setSourceFile('../pdf/files/chart_template.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('Arial', 'B');
$pdf->SetFontSize(7);
while($rows=mysql_fetch_array($result)){
$pdf->Cell(20,5,$rows['itemname'],0,0,'C');
$pdf->Ln(45);
}
$pdf->useTemplate($tplIdx);
$pdf->Output('file.pdf', 'I');
?>
Please help!
I've done it previously using the 'header' class extension:
class PDF extends FPDF
{
function Header()
{
//Select Arial bold 15
$this->SetFont('Arial','B',15);
//Move to the right
$this->Cell(80);
//Framed title
$this->Cell(30,10,'Title',1,0,'C');
//Line break
$this->Ln(20);
}
Have a look at the tutorial which explains the header usage at : http://www.fpdf.org/en/tutorial/tuto2.htm