Print PDF file with variables from other file? - php

I am wondering is it possible to print PDF file, and replace the blank spots in it with variables I get from my website, so output depends on the variable's value etc.

Use FPDF to do what you need.
But in this case you have to know that you must create a pdf file from scratch and fill it in the way you want.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage(); // add page to PDF
$pdf->SetFont('Arial','B',16); // Choose a font and size
$pdf->Cell(40,10,'Hello World!'); // write anything to any line you want
$pdf->Output("your_name.pdf"); // Export the file and send in to browser
?>

Related

php fpdf simple file is not working

I have a simple app where the php file updates the database then it generates a pdf file and stores in local folder. below is the code. the pdf file is getting generated and working fine. but am not able to add text into it. when i insert image its working. but inserting cell or text is not working. i dont know whats gone wrong.. pl;z help me.. thanks
$pdf = new FPDF();
$pdf->AddPage();
// $pdf->Image('images/logo.png',90,15,90,0,'PNG');
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output("pdffiles/testsuccess.pdf",'F');
When I test your code it works fine for me. The only thing I DON'T see in your code is the require statement for the fpdf.php file.
heres the FULL php file contents for the example that works for me:
<?php
require('fpdf/fpdf.php'); // MAKE SURE YOU HAVE THIS LINE
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output("testsuccess.pdf",'F');
?>
Finally, I found it.. weird but it will be very useful for the developers who is new to fpdf... I have mentioned that image is working and only text is not working.. Means you should include the fonts folder which comes with fpdf when downloading. If fonts folder is missing it does not print any text..
Thank you

Execute two fpdf pdf files at once?

I have a requirement where two pdfs need to be created at once. A for loop will not work as one file is going to be a certificate pdf. The next file is going to be an Invoice pdf. I know I am missing something simple or else its not possible to do this. I have attached a simple code to demonstrate what I mean. The files are executed on a form submission.I have made two files and then a handler file to include the two files but still only outputs on pdf.
<?php
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Im a certificate');
$pdf->Output('Certificate.pdf', 'D'); //Output of the first invoice
$pdf = new FPDF(); // This is going to be the second pdf the Invoice
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Im an invoice');
$pdf->Output('This is a invoice.pdf', 'D');
//The problem is it only outputs the first one not this one
?>

How can i upload a pdf page generated with a PHP code?

I have a php page that creates a pdf file and output its on browser.
I want my php page to upload it on a webserver (cPanel). i've found lots of sites explaining how to upload file througt form and file type but i can't do this. Anyone knows how to?
You can use fpdf to do this easily.
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output('doc.pdf','F');
This will save the pdf with content Hello World as its content in the public_html directory of your webserver.
Or you could use
file_put_contents("fileName.pdf", fopen("http://your.url.com/file.pdf", 'r'));

codeigniter and fpdf code blockin during pdf output

i have a big problem with fpdf
in my page i have a lot of code and at the end i create a pdf file with this sample:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
ob_end_clean();
$pdf->Output();
the result is that only the creatioin of pdf are make and the other code no !
first the code of the sample is:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
//ob_end_clean();
$pdf->Output();
like this the code before is execute without problem but not the pdf and i receive this on browser:
FPDF error: Some data has already been output, can't send PDF file
can you help me ?
im newbie with fpdf libraries
that is because your php code has already outputted some data to the browser.
Check that your php do not write any data to the browser yet ( except for the headers ) before calling $pdf->Output(), thus avoiding sending unknown data that has a pdf binary data appended at the end.
the ob_end_clean() function call ensure that your script clean any previous output your php script could have made.

how to generate pdf and save to server

Well, i'm trying to generate pdf file and save it to server but when i'm trying to view that pdf its showing invalid format.
Code :
<?php
$content = "32w434";
file_put_contents("xyz.pdf",$content);
?>
If i change file from pdf to doc it works and opening perfectly but not pdf
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output();
file_put_contents("xyz.pdf",$content);
That code giving output i don't want output on browser and still file is corrupted / invalid format
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output('doc.pdf','F');
http://www.fpdf.org/
You can use FPDF to generate pdf file....
http://www.fpdf.org/
This is simple and best way to generate pdf...
Use TCPDF to generate PDF file,
TCPDF Site: http://www.tcpdf.org/
Demo: http://www.tcpdf.org/examples.php
#Mohit,
This
$content = $pdf->Output('doc.pdf','F');
generates a new file. If your file is generated using variables that can change in php, the pdf file you see on screen, will be different from the pdf file saved on the server.
You should generate pdf file using pdf libraries such as tcpdf or fpdf. Putting contents directly into a file with .pdf extension will definitely not working since pdf has its own file format

Categories