PDF created with FPDF cannot be opened by Adobe Reader - php

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

Related

I cannot open pdf in chrome created by Mpdf - CodeIgniter . - Failed to load PDF document

I am new to PHP report generation. I am trying to use MPDF 5.7. When I try create sample pdf using simple php page, it is created successfully. But when I put it in to codeigniter chrome browser says "Failed to load PDF document.". But It can open using firefox. But if i downloaded it, it is not support to open using adobe reader. But still my sample pfd is working well anyway.
This is how I create sample pdf.
<?php
$html = '
<h1>mPDF</h1>
<h2>Basic Example Using CSS Styles</h2>
<p class="breadcrumb">Chapter ยป Topic</p>
<h3 style="color:red; background-color:gray; margin-left:100px;">Heading 3</h3>';
include("themes/MPDF57/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyleA4.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
When I put it into codeigniter cannot open pdf. my php file is created in 'view' and mpdf library also put in same place in theme folder.
Your PDF file contains some direct PHP output from your script which makes it corrupt.
It can be a whitespace from after the ?> PHP closing tag, it can be PHP notice printed during course of generating the file, etc.
Be sure that you disable all layouting your framework provides as it produces the exact same output that could cause this.
To troubleshoot further, save your PDF file to disc ($mpdf->Output('<file path>', "D");) and open it in a text editor. You will see a bunch of weird characters.
If the file does not start with %PDF-1.4, look for the reason of the output before calling $mpdf->Output();.
If there is some readable text at the end of the document, look after calling the Output method.
I'd suggest you to remove your ?> PHP closing tag anyway for a good measure.
See also https://mpdf.github.io/troubleshooting/error-messages.html
The reason this happens is that there are some PHP errors/warnings that show up before the PDF gets written, these make it a non-valid PDF therefore not readable by the Browser.
You can see it by downloading and opening the PDF file with a TextEditor and you'll see PHP errors instead of the standard %PDF-1.4 value (which should be the first value of the file).
If you suppress warnings error_reporting(E_ERROR | E_PARSE); you should be good to go.
Try this
$mpdf->Output('', "I");
so then file should be displayed inline in browser instead of downloading it.
if you want to download use "D" insted of "I"
I had the same problem, I didn't notice that header and footer (header was the problem) are still included in my index file. When I called my generate_pdf file it was pulling header and that created error. After I removed header pdf was generated successfully.

PDF manipulation - images are distorted after few consecutive operations on PDF file

I've run into this weird issue with PDF file handling. Not sure if SO is the right place to ask this, but I couldn't find any specific sites for this. I hope that someone can shed some light on the issue.
This happens with the following specific process, if some of steps are omitted - the issue is not observed.
I have a PHP application that serves PDF files to users. These files are created by authors in MS Word 2007, then printed to protected PDF (using pdf995, most likely, I can confirm if needed).
I'll call this initial PDF file as 'source' hereinafter.
Upon request, the source file is processed in PHP the following way:
we decrypt it using qpdf:
qpdf --decrypt "source.pdf" "tmp_output.pdf"
Then we add security label / wartermark to it, encrypt and output to browser using mPDF 6.0:
$mpdf = new mPDF();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile($fpath);
if ($pagecount) {
for ($i=1;$i<=$pagecount;$i++){
$tplId = $mpdf->ImportPage($i);
$mpdf->UseTemplate($tplId);
$html = '[security label / watermark contents...]';
$mpdf->WriteHTML($html);
}
}
$mpdf->SetProtection(array('copy','print'), '', 'password',128);
$mpdf->Output('final_output.pdf','I');
With the exact steps described above, images in the output that were pasted in the Word doc appear as follows:
In the source PDF, tmp_output (qpdf decrypted file) the pasted images look correct:
The distortion doesn't take place if any of the following occurs:
Word doc printed to PDF without protection
mPDF output is not protected.
As you can see there too many factors, so I don't know where to look for a bug.
Each component works correctly on it's own and I cannot find any info on the issue. Any insights are greatly appreciated.
EDIT 1
After some more testing, it appears that this only happens to screenshots taken from web browser, Windows explorer, MS Word. Cannot reproduce this with screenshots from Gimp.
It appears that something along the way attempts to convert white to alpha and fails.
The current version (6.1) of Mpdf has a bug which does not handle escaped PDF strings (imported via FPDI) correct if they should be encrypted.
A pull request, which fixes this issue is available here.

Can't download PDF (FPDF) from PHP function

I am working on a PHP MVC application right now and I am having some troubles with FPDF. The code is pretty simple, but it won't download the PDF file (Also tried to open it and it doesn't work). I think I am missing something or maybe I need to disable something from the fpdf instance, but I can't reach the problem to solve it. Here's the code:
public function downloadPDF(){
require('../FPDF/fpdf.php');
$pdf = new FPDF();
$numero = cal_days_in_month(CAL_GREGORIAN,8,2003);
date_default_timezone_set("Atlantic/Canary");
setlocale(LC_ALL,"es_ES");
$pdf->SetFont("Arial","",14);
$pdf->AddPage();
$mifecha = gmmktime(0,0,0,2,1,2013);
$pdf->Cell(40,10,strtoupper(strftime("%B",$mifecha)),1);
$pdf->Ln();
$pdf->SetFont("Arial","",10);
$pdf->Cell(60,10,"By Ricky",0,1,"C");
$pdf->Output($pdf,"D");
}
Just in case: the idea is that using this function will automatically download the PDF file. For example, when clicking a "Download PDF" button in my menu.
It's almost a problem of header() somewhere into your MVC:
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
if you set correctly those, you should be alright.
Remember that header() must be called before any input or it won't work
OK, it was such an obvious mistake I didn't notice. I was using this:
$pdf->Output($pdf,"D");
FPDF's Output function receives two string parameters (name, output type). Instead of giving this function a name string, I was giving it the $pdf object instead. It worked perfectly when I fixed it, it ended up like this:
$pdf->Output("PDF name","I");
By the way, thanks for your support.

PHPRtfLite - RTF file opens as raw

I am using PHPRtfLite library (http://sigma-scripts.de/phprtflite/docs/index.html) to produce an RTF file using PHP and Yii.
So far, I've made a simple "Hello world" function.
Yii::import('ext.phprtf.PHPRtfLite');
Yii::registerAutoloader(array('PHPRtfLite','registerAutoloader'), true);
$rtf = new PHPRtfLite();
$sect = $rtf->addSection();
$sect->writeText('Hello world!', new PHPRtfLite_Font(), new PHPRtfLite_ParFormat());
//save rtf document
$rtf->sendRtf('takis.rtf');
File is created successfully, but when I open it (either wordpad or ms word) I do not see the actual content of the file but the raw code of the RTF:
{\rtf\ansi\deff0\fs20
{\fonttbl{\f0 Times New Roman;}}
{\colortbl;\red0\green0\blue0;}
{\info
}
\paperw11907 \paperh16840 \deftab1298 \margl1701 \margr1701 \margt567 \margb1134 \pgnstart1\ftnnar \aftnnrlc \ftnstart1 \aftnstart1
\pard \ql {\fs20 Hello world!}
}
Do you have any idea on how to solve this?
Thank you very much in advance.
To answer my own question, in case someone is having the same issue in the coming future...
It seems to be a problem of the sendRTF function. Now, I save the created file locally:
$rtf->save('takis.rtf');
and then generate a link for the user to download the file. This works pretty good.
I have experienced same thing myself. I'm not sure, if you had same reasons, but in my case, there was extra newline in the beginning of PHP file, before <?php tag. When I used sendRtf to download file from browser, that newline ended up also in RTF file, making it invalid and as result, raw rtf code was displayed. When using save, such extra characters won't reach to file.
So one thing to check in similar situations - open Rtf file in Notepad and examine beginning of file.

Export a html page to pdf with everything that's written on it, after submit button

I need to export html page to pdf file with everything that's written in it, after I press submit button. It will open new page, with info, and I need for script to automatically make .pdf file (already uploaded to webserver), and get the link from file. Could you give me some easy example (if available, without any plugins, or other features that I must download, I would prefer clean PHP).
Just try this
HTML to PDF with PHP
Using open-source HTML2FPDF project
This solution uses HTML2PDF project (sourceforge.net/projects/html2fpdf/). It simply gets a HTML text and generates a PDF file. This project is based upon FPDF script (www.fpdf.org), which is pure PHP, not using the PDFlib or other third party library. Download [HTML2PDF][1], add it to your projects and you can start coding.
Code example
require("html2fpdf.php");
$htmlFile = "your link";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');

Categories