I'm creating a pdf from formdata using fpdf (http://www.fpdf.org/).
Using Jquery I'm able to send the data to the pdf-creating script, and make the pdf appear in browser.
Now I'm trying to return the pdf-data to the formpage using fpdf's option to get the pdf as a string and echoing it.
$pdf_file_contents = $pdf->Output("","S");
echo $pdf_file_contents
My question now is
How do I use this data to preview the pdf under the form?
Do I need to use an pdf-viewer or can I use an <iframe> and am I doing this the correct way by returning the variable in an echo?
I can think of two options:
Embed a PDF viewer and handle that call in JS. (Wait for the response from PHP and display the outputted file inline)
Trigger a redirect or load an iframe which displays $pdf->Output($filename,"F") from a temp file.
The result of $pdf->Output() is displayed using the browser's built-in viewer. If there is any output above that in the page FPDF will return an error to say FPDF error: Some data has already been output, can't send PDF.
FPDF error: Some data has already been output, can't send PDF
There might be some other option I'm missing but those would be my first suggestions.
Related
Hi people when i use this code it saves the pdf file without problem. But when i use: $pdf->Output("pdf_test.pdf", "I");. It gives me :
FPDF error: Some data has already been output, can't send PDF file
thank for helping
So my application generates pdf files using TCPDF, and that works fine. That is done inside php file, called with ajax. I'm using embed tag to preview them like:
$.ajax({
url: 'create_pdf_file.php',
success: function(){
/* https://stackoverflow.com/questions/17083018/jquery-loads-cached-file-inside-ajax-success-function */
$('#pdf_placeholder embed').attr('src','output/my_file.pdf?v=' + Math.random()');
}
});
Because many users could generate my_file.pdf at the same time, there could be a case when one user will preview a file generated for another user. So my question at this point is how to force TCPDF to output directly into that tag, not using temp file
Output('my_file','I')
not working here after ajax.
There is an advice here to echo pdf directly back, but i don't know how to do that or is it possible at all. Anyway will try with success(data) to receive that like json.
Other workaround is to give session-bond file name for each user, but is using session_id() and appending to the file name is safe? Most probably I will end with generating UID for each session.
Any general advices are welcome.
You should try passing what TCPDF will produce in a way widely used in embeding images: Data Uri
Embedding Base64 Images
This however might choke browser - i haven't test this.
I would rather save pdf file on server, print filename to browser or other ID of produced file so it could be read with "success" of the ajax request. Then calmly pass proper filename to Embed element. If you wish to do it more safely you can encode it using already used session-exclusive data like session cookie or data that is assosiated with that cookie on a server. You can bond pdf file access to IP that sent request to produce it and timestamp of request that caused production.
I am done with generating PDF file using FPDF in php. But the problem is how to open this pdf without the Save As option? I want to display the pdf document in the browser.
http://www.fpdf.org/en/doc/output.htm
Syntax: Output([string name] , string dest) , use I as Destination and fdpf will try to show it in the browser, if browser plugings and so on enable it
You cannot force this display, as it is up to the user to choose to display the PDF inline or systematically save them. I prefer the second option...
Now, there is a JavaScript / HTML 5 project (experimental!) to display PDF without plugin, so perhaps you can try that.
Even when using fpdf passing the output to the browser, I believe its still up to the user if they open or save it.
A solution would be to use some kind of PDF viewer, for example http://view.samurajdata.se/
Try this $pdf->Output('I', 'filename.pdf')
See the reference http://www.fpdf.org/en/doc/output.htm
Set header's content-type to 'application/pdf'. Then, most browsers will try to open it and show in-browser (or at least ask user to save or open file)
Your browser must have pdf plugin installed. If you havent done so install latest version of Acrobat Reader. If you are using fpdf, output the string instead of forcing download
For details
http://www.fpdf.org/en/doc/output.htm
Try echoing the PDF instead to using header function. The header function will force the browser to download. The echo 'might' show the pdf.
I have a website now and I want to create a button on it to convert this page to PDF.
Is there any code to make this happen? I cannot find it on the internet.
So I want to have a button and when I press on it it converts the page to a .PDF file.
I do not want to use a third party website to generate the PDF's. I want to use it for internal purposes to generate files with PHP. So I need the code what can make a PDF for each page.
I use wkhtmltopdf - works very well - http://code.google.com/p/wkhtmltopdf/ there is a PHP wrapper
Updated based on comments below on usage :
How to use the integration class:
require_once('wkhtmltopdf/wkhtmltopdf.php'); // Ensure this path is correct !
$html = file_get_contents("http://www.google.com");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');
Use FPDF. It's a well-respected PDF-generating library for PHP that is written in pure PHP (so installing it should be dead simple for you).
Try this:
http://www.macronimous.com/resources/Converting_HTML2PDF_using_PHP.asp
It will convert HTML to a PDF using FPDF and HTML2PDF class.
Also found this:
http://www.phpclasses.org/package/3168-PHP-Generate-PDF-documents-from-HTML-pages.html
I'm trying to user HTML2FPDF (http://html2fpdf.sourceforge.net/) to create a PDF of a page, but I can't seem to get it to work properly.
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.
HTML2FPDF code:
function createPDF() {
define('ABSPATH', dirname(__FILE__).'/');
require(ABSPATH.'classes/pdf/html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$html = ob_get_contents();
//$html = htmlspecialchars($html);
if ($html) {
$fileName = "testing.pdf";
$pdf->WriteHTML($html);
$pdf->Output("pdfs/".$fileName);
echo "<p>PDF file is generated successfully! Click here to open it.</p>";
} else {
echo "<p>There has been an error in creating your PDF.</p>";
};
};
If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?
Cheers
A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.
The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.
I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.
The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.
Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.
This would work, but it might be a bit much.
Regards.