I know this question has been asked many times but I read all related answer and my problem is not solved yet. I can add text and image on a new blank pdf file by code below.
<?Php
require('fpdf.php');
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
/* $pdf->setSourceFile("test1.pdf"); */
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(0, 5);
$pdf->Write(0, 'This is just a simple text');
$pdf->Image('sample.png',100,0);
$pdf->Output('file.pdf','F');
?>
But when I uncomment the line /* $pdf->setSourceFile("sourse.pdf"); */ to add text and image on existing pdf, I get Uncaught Error: Call to undefined method FPDF::setSourceFile() .
Also when add
require('fpdi.php');
$pdf = new FPDI('P','mm','A4');
I get Uncaught Error: Class "setasign\Fpdi\FpdfTpl" not found .
How can I solve this?
You have to set source file and after that import page from source file to your new created file by this way you can edit it. Please find more details from https://www.webniraj.com/2016/09/12/creating-editing-a-pdf-using-php/
// Create new Landscape PDF
$pdf = new FPDI('l');
// Reference the PDF you want to use (use relative path)
$pagecount = $pdf->setSourceFile( 'certificate.pdf' );
// Import the first page from the PDF and add to dynamic PDF
$tpl = $pdf->importPage(1);
$pdf->AddPage();
// Use the imported page as the template
$pdf->useTemplate($tpl);
// render PDF to browser
$pdf->Output();
Thanks in advance...
I am trying to generate a pdf in CodeIgniter using the fpdi and tcpdf libraries using the following code...
<?php
use setasign\Fpdi;
require_once('tcpdf/tcpdf.php');
require_once('fpdi2/autoload.php');
class Pdf extends Fpdi\Tcpdf\Fpdi
{
/**
* "Remembers" the template id of the imported page
*/
protected $logo;
/**
* Draw an imported PDF logo on every page
*/
function Header()
{
if ($this->logo === null) {
$this->setSourceFile(base_url().'assets/pdf/logo.pdf'); //Will work if these 3 lines are commented
$this->logo = $this->importPage(1); //Will work if these 3 lines are commented
}
$size = $this->useImportedPage($this->logo, 130, 5, 60); //Will work if these 3 lines are commented
$this->SetFont('freesans', 'B', 20);
$this->SetTextColor(0);
$this->SetXY(PDF_MARGIN_LEFT, 5);
$this->Cell(0, 30, 'TCPDF and FPDI');
}
}
// initiate PDF
$pdf = new Pdf();
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
// add a page
$pdf->AddPage();
$pdf->Write(5, "hello world");
$pdf->Output('generated.pdf', 'I');
?>
It runs and generates the pdf if I comment the lines I mention in the inline comments, but generates the error:
Type: InvalidArgumentException Message: Given stream is not seekable!
when I uncomment those lines.
The code works in my local server outside codeigniter.
I am able to embed the pdf in another view, so it is not a problem with the pdf nor with a restriction of accessing pdfs from my assets folder.
The same problem happens when I try to load an external font.
The view is being called by the controller this way...
class Projects extends CI_Controller{
public function fpdi(){
$this->load->view('projects/fpdi');
}
}
Thanks a lot!
(note that this is an oversimplification of the problem, I am not loading the libraries inside the view, this is just to make the problem as concise as possible for demonstration purposes)
edit-UPDATE tried the same thing with fpdf and I still get the same error "Given stream is not seekable".
UPDATE 2! Thanks to Jan Slabon's advice I kind of solved it, a replaced base_url() helper with CodeIgniter constant FCPATH so the path was relative and not a URL. Apparently this is required for fopen to function properly which is used by the fpdi library.
I don't know more and I feel this is inelegant solution but it works for now! If anybody has more info it would be greatly appreciated.
Changed the source from base_url() to relative path FCPATH. (More info at the bottom of question.)
I am trying to create hundreds of PDF files with TCPDF using a while loop. The problem is that it seems to create only the first one and then stops. I turned on error reporting and there is no error.
I basically get the data with:
ob_start();
while($row = mysqli_fetch_assoc($data)) {
$invoice_id = $row['invoice_id'];
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new MYCUSTOMPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// All $pdf-> settings are here
// add a page
$pdf->AddPage();
$pdf->lastPage();
$pdf->Output('/path/to/'.$invoice_id.'.pdf', 'F');
echo "Generated ". $invoice_id."\n";
}
The .PDF gets generated fine, but it stops there. It doesn't continue with the next in the loop.
Anybody with TCPDF experience knows what is wrong? I can't figure it out and there is no error even with error_reporting(E_ALL);. Any help is greatly appreciated.
I have html, when I run that html it is working well. But, when I want to create pdf of that html, design changes and I am not getting what design I have.
Also I can not add pages, my design has two page and I want the pdf to be in two pages.
here is my sample code:
require_once('eng.php');
require_once('tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('RoyalHome.ae');
$pdf->SetTitle('Listing ');
$pdf->SetSubject('PDF of Listings');
$pdf->SetKeywords('Royalhome, PDF, listing');
$pdf->SetFont('Helvetica', 'B', 10);
// add a page
$pdf->AddPage();
my html is here fiddle
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->SetFillColor(255,255,0);
$pdf->lastPage();
$sr = $fileName . '.pdf';
$pdf->Output($sr, 'D');
Just tested your code. Ran into the following issues:
You have relative URLs for img tags, which, on my machine, results in tcpdf throwing an error when trying to fetch the images. Make sure you use absolute URLs to avoid this.
You are using the D flag for Output destination. I got errors because headers were already sent. If you are generating and outputting the pdf after the page loads, or if errors are thrown and output to screen, the output will fail.
I removed all <img /> elements and changed the destination flag to F and the pdf generated just fine on my desktop.
I am using the fpdf library for my project, and I'm using this to extend one of the drupal module. These lines
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
give me an error: FPDF error: Some data has already been output, can't send PDF
I tried creating this in a separate file outside the drupal area name test.php and when viewed it worked. Anyone here know why this don't work? Or anyone here can point me a right pdf library which I can use in drupal to view HTML to PDF format.
For fpdf to work properly, there cannot be any output at all beside what fpdf generates. For example, this will work:
<?php
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
While this will not (note the leading space before the opening <? tag)
<?php
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Also, this will not work either (the echo will break it):
<?php
echo "About to create pdf";
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
I'm not sure about the drupal side of things, but I know that absolutely zero non-fpdf output is a requirement for fpdf to work.
add ob_start (); at the top and at the end add ob_end_flush();
<?php
ob_start();
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
ob_end_flush();
?>
give me an error as below:
FPDF error: Some data has already been output, can't send PDF
to over come this error:
go to fpdf.php in that,goto line number 996
function Output($name='', $dest='')
after that make changes like this:
function Output($name='', $dest='') {
ob_clean(); //Output PDF to so
Try to save the file without the option: "BOM comment", i.e. in Adobe Dreamweaver, you Save File As..., uncheck the box below the filename that says, "Include Unicode signature(BOM)".
On Notepad++ you should select the menu: Encoding, "Encode in UTF-8 without BOM".
And make it default for other files you create, it will spare you a lot of headaches in future.
Hi do you have a session header on the top of your page.
or any includes
If you have then try to add this codes on top pf your page it should works fine.
<?
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);
?>
cheers :-)
In my case i had set:
ini_set('display_errors', 'on');
error_reporting(E_ALL | E_STRICT);
When i made the request to generate the report, some warnings were displayed in the browser (like the usage of deprecated functions).
Turning off the display_errors option, the report was generated successfully.
The FPDF Error Message will point you to the PHP Line that is sending some content.
If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.
For me
fpdf.php was ANSI-encoded,
my pdf-generator.php was UTF-8-encoded and
my database-connect-inlude was UTF-8-encoded (this UTF-8-encoding did raise the FPDF error. I had to switch it back to ANSI)
if you're code outputs notices/warnings before the PDF generation, try turning them off. error_reporting(0). Then work on the warnings there-after
Add to the beginning of the script
ob_start();
require ('fpdf.php');
and at the end, after output()
ob_end_flush();
It worked for me! =)
First step
check the permissions on the folders
second step
put this
ob_start();
before the line
$pdf->Output();
I used the following and it worked for me
require_once ('pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output(F,'/var/www/html/PATH/filename.pdf');
ob_end_flush();
Even a single space in the included php files causes that warning. There shouldn't be any output in any way.
You need to call the library
require ('fpdf.php');
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'¡Hola, Mundo!');
$pdf->Output();
?>
http://www.fpdf.org/
http://www.fpdf.org/es/tutorial/tuto1.htm
Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file (output started at /home/asri123/public_html/bill/invoice/invoice.php:743)' in /home/asri123/public_html/bill/invoice/fpdf.php:271 Stack trace: #0 /home/asri123/public_html/bill/invoice/fpdf.php(1052): FPDF->Error('Some data has a...') #1 /home/asri123/public_html/bill/invoice/fpdf.php(1012): FPDF->_checkoutput() #2 /home/asri123/public_html/bill/invoice/mirasbill.php(262): FPDF->Output('MSFS/2018-19/76...', 'D') #3 {main} thrown in /home/asri123/public_html/bill/invoice/fpdf.php on line 271
Another answer that nobody else has posted here... Double-check the encoding of your PHP file and make sure that it's not something other than UTF-8. The wrong code editor (or FTP upload?) can mess with the file's encoding in which case none of the other fixes mentioned in this thread will help.