When i start it's showing this error
<?php
$p = PDF_new();
?>
Fatal error: Call to undefined function PDF_new() in D:\wamp\www\upload.php on line 2
I am using Wamp Server. I tried in XAMPP also. Is there any directives i have to enable to execute the code ?
i suggest to you tcpdf. it was good for me.
some feature:
no external libraries are required
for the basic functions;
all standard page formats, custom
page formats, custom margins and
units of measure;
UTF-8 Unicode and Right-To-Left
languages;
I think http://www.fpdf.org/ is the best PDF library for PHP.
Download latest version from http://www.fpdf.org.
Put this library folder on your root server or in your project.
Create on test file named test.php & put below code in file as below.
<?php
include("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',36);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
It will create one pdf file with contents "Hello World!" in it.
You are done..
Related
I'm creating a web app that allows a canvas form to insert an image from a HTML canvas into a particular position in multiple PDF files. I had this working with python flask as a back-end but the people that I'm making it for only want it in PHP. I have tried using libraries like FPDI but they only work with PDF versions up to 1.4 while the PDF files we are using are version 1.7.
Does anyone know any possible libraries that can help me solve this issue. I would prefer not to convert the PDF files if possible.
Cheers
With TCPDF you can insert images into a PDF (v.1.7) file:
Requirements
composer require tecnickcom/tcpdf
Example
<?php
require_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF();
$pdf->setPDFVersion('1.7');
$pdf->setAutoPageBreak(true);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
// Insert image
$pdf->setJPEGQuality(100);
$pdf->image(__DIR__ . '/example.jpg', 10, 10);
// Close and output PDF document
//$pdf->output('doc.pdf', 'I');
// Save the pdf file
$content = $pdf->output('', 'S');
file_put_contents('example.pdf', $content);
We (Setasign, creator of FPDI) offer a commercial add-on that let you import PDFs which uses a compression technic that was introduced in PDF 1.5.
You may also try to downgrade these documents with an external program. I'm aware of some people using Ghostscript for this.
Generally you should know that you do not insert an image into the existing PDF but you create a completely new PDF while importing a single page into a reusable structure which you place onto a newly created page. On top of this you place the image.
With FPDI you cannot edit a PDF document.
I switched from TCPDF to domPDF because it seems more convenient to handle when creating invoices from html to pdf (I am rather a low pro on PHP :)). Now that I created the html file as a PDF file I recognized it does not output any PHP in the PDF - since the data from my sql databanks should fill the PDF it is kinda a problem.
I saw that you can enable PHP in the options.php included in the src-folder and I tried to do like it is written in the manual (and also tried various other code lines) but it just doesn't want to work:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once ("$root/../xxx/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->setIsPhpEnabled('true');
$dompdf = new Dompdf($options);
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("bla",array("Attachment"=>0));
The PDF is shown but without the input from any PHP code.
If someone would be so kind, I would also be interested in knowing why and in how far enabling PHP is a security risk since I actually want to use that for my business. Would it be more advisable to wrap it all up in the main php file without loading external html and css files?
Thanks a lot in advance!
You could do something like this (not tested the code). Replace
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
With
ob_start();
include 'testdomhtml.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
More options How to execute and get content of a .php file in a variable?
Your file_get_contents("testdomhtml.php") will get actual content of file and will not execute any code inside it. Instead make it web accessible and pass URL to this page:
$dompdf->load_html_file('http://yourdomain.ext/testdomhtml.php');
I create a PDF in Moodle using the following code
$pdf = new pdf;
$pdf->AddPage();
$pdf->Write(1, "Test");
$pdf->Output();
How would I make this download in the browser instead of opening in browser?
// Force the browser to download the output
$pdf->Output('filename.pdf','D');
Moodle wraps the TCPDF library for PDF generation (the wrapper mostly just handles the locations for temporary files and accessing embedded images which are in the Moodle Files API).
You can find documentation about the TCPDF Output() function online at http://www.tcpdf.org/doc/code/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
The important param is the second one, calling $pdf->Output('filename.pdf', 'D') will cause it to download.
My PDF file cannot be opened with Adobe Reader. What's weird is that the PDF used to work in Adobe Reader just a few days ago and now it does not work, and of course I didn't modify the code otherwise it would be easy to fix.
I validated my PDF and learned it was a PDF/A-3, could be that it is not a PDF/A-1? If so why did it work for a time and suddenly stopped working?
EDIT
The problem was that somehow, some HTML code was output inside the PDF file, thus making the PDF file invalid for Adobe Reader, but not for Firefox, making all this much more confusing than it should be.
So a tip to anyway having trouble with PDF, open it with a text editor such as Notepad++ for Windows or GEdit for Linux, and compare with another PDF file which works fine. You should find the problem pretty quickly.
I read a lot of questions about this and I understood that the problem is happening with:
ob_start();
Without this line, my FPDF was not working with the message explained that there was another buffer first.
With this line, the PDF was readable only with Browser.
I changed this line to:
ob_clean();
and now I can open PDF with any reader.
This was happening to me, too. It did work in Firefox for me, but not in Chrome and it wouldn't open in Adobe.
The problem was that I was trying to run the script in a function and calling the function from a button. I fixed it simply by adding the script to it's own PHP file, and then linking to the php file directly.
Note: I did try ob_start() (and alternatively ob_clean() as noted in the other answer) and ob_end_flush(), but it didn't make a difference in the function. When it's in its own file it doesn't need it anyway.
// file.php
<?php
require $_SERVER['DOCUMENT_ROOT'].'/wp-content/plugins/eri-webtools-plugin/libraries/fpdf/fpdf.php'; // <-- File path for WordPress plugin
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output(); // To Download, use $pdf->Output('D', 'test.pdf', true);
?>
// html
View PDF
I want to generate a PDF file in my web app. I am using php and I tried fpdf library
I tried to create pdf by using following code:
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Привет мир!');
$pdf->Output();
?>
It works when we use english characters inside file, but file encoding is ANSI and inside writen russian characters I get that error:
if I change file encoding to utf-8 get error:
FPDF error: Some data has already been output, can't send PDF file (output started at Z:\home\fpdf\www\tutorial\tuto1.php:1)
Who knows how to resolve the problem, please help me !
$pdf->Cell(40,10, iconv('UTF-8', 'CP-1251', $str) );
Make sure you're saving your document as UTF8