I'm using a Google project API to convert HTML to PDF. Works great and is amazing. You can get it here if you want:
http://code.google.com/p/dompdf/
My question is as follows, the plugin does it's magic and I obtain something like this:
file_put_contents("nameOfPdf.pdf", $pdf);
How do I go from this to saving it to my server? I'm new to saving info to server, so please detail a bit.
Thanks.
MORE OF THE CODE HERE
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper( "a4" , "portrait");
$dompdf->render();
$dompdf->stream("xxxxx.pdf", array("Attachment" => false));
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it
// to the client.
file_put_contents("xxxxx.pdf", $pdf);
If I read the docs correctly, you need to remove this line:
$dompdf->stream("xxxxx.pdf", array("Attachment" => false));
It will then save it to disk without any output.
To specify a path, add it where you show the file name in the file_put_contents call.
Related
I am using dompdf php lib for generating pdf.
Right now it ask user to save the file in their machine.
Code i tried so far:
<?php
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream($filename.'.pdf');
$output = $dompdf->output();
file_put_contents($filename.'.pdf', $output);
?>
Give "write" permission to the directory you need to put the file.
I have read lots of questions about this topic, but none of their solutions have been useful to me. I am trying to upload a pdf file created by DOMFILE to my server, but I get 500 error.
I have looked in my access_log and error_log but there is nothing. I have also checked permissions through Filezilla, and they are correct (please, tell me if I have done it well):
I have made my code very simple just to focus on this problem:
<?php
require_once "dompdf/autoload.inc.php";
$html =
'<html><body>'.
'<p>Some data to be transformed to pdf.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper( "a4" , "portrait");
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents("test.pdf", $pdf);
?>
I need your help to fix this:
My current code is this:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$html = 'Insert full HTML content';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codexworld",array("Attachment"=>0));
?>
This is the error a get:
Why if I have exactly the same code like the basic example, am I getting this error? What am I missing? I don't find in my folder the Autoloader.php, where do I have to get that file?
Well apparently it's an issue after domPdf has moved to Github. It seems that php-font-lib library doesn't exist. So one solution is to manually download it:
Go to https://github.com/PhenX/php-font-lib and download the library.
In the zip file, take the contents of the src/FontLib/ folder and paste that into the folder lib/php-font-lib.
or you can check this answer here
i converted my html to Pdf using dompdf and i want to print it. My problem is how to open my pdf file in print window instead of opening the download dialog?
include('dompdf/dompdf_config.inc.php');
$savein = 'pdfdirectory/';
$html = " my htmlcode here "
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents(($savein.'file.pdf'), $pdf);
$dompdf->stream('file.pdf');
You can't force the user to open the file in their browser, but you can specify options for the stream() method that hint to the browser whether or not to download the file. The default is to tell the browser that the file is an "attachment" which usually translates to a download. But if you modify the last line of your code as follows most browsers should display the PDF in the browser:
$dompdf->stream( 'file.pdf' , array( 'Attachment'=>0 ) );
After creating a PDF file in PHP Using DOMPDF, I need to redirect user to different page.
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($fileName . '.pdf', array("Attachment" => 0));
header('location:newpage.php');
This code creates the PDF file but does not redirect. How can I fix this problem?
Note : This is my previous question. Still I'm trying to get this fixed, still no luck.
The $dompdf->stream() call starts outputting data to the client. Once content is being sent, it is impossible to modify the headers, and thus perform the redirection you want. You can see this by turning on the warnings in PHP (error_reporting).
A way to do this is to open a new window, but it requires Javascript.
function downloadPDF(){
window.open("get-pdf.php?id=12345");
location.href = "newpage.php";
}