On Fri, May 22, 2009 at 11:21 AM, Dee Ayy <[email protected]> wrote:
> Kyle,
> Well I guess that is good news. But I don't trust these headers since
> the filename is not being set. But that was even before this IE
> issue.
>
> Bastien,
> I don't understand how I could "save the file to the hard disk" from
> IE. But yes, I save the file (it gets saved as my_php_file.php on the
> hard disk when using FF) which I then rename to my_php_file.pdf, and
> then I open that in a PDF viewer. But IE claims that the "site is
> either unavailable or cannot be found".
>
Why are you saving the file first as PHP and then renaming to PDF?
Below is the code that I use (which uses the DOMPDF class from
digitaljunkies.ca
<?php
/*
createPDF creates a pdf and streams it to the user if FF / O / S or saves
it and forces a download if IE
$html -> the html page used to create the content
$thisFileName -> is the name of the file to be saved
$hash -> hash of the user to use as the path to the save the file
to
*/
function createPDF($html,$thisFileName,$hash)
{
require_once("includes/dompdf_config.inc.php");
// Turn off all error reporting
//error_reporting(0);
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
// The next call will store the entire PDF as a string in $pdf
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it
// to the client.
//check if IE,since it doesn't like the stream, so we save it to disk and
then stream the saved file
$browserCheck = browserDetection();
if ($browserCheck)
{
if (!is_dir("../data/$hash"))
{
mkdir("../data/$hash");
} // end is_dir check
// save the file
if(!file_put_contents("../data/$hash/$thisFileName", $pdf))
{
echo "failed to write new pdf file";
}
// output to the browser
showPDF("../data/$hash/$thisFileName");
}else{
$dompdf->stream("$thisFileName");
}// if user agent
}//end function createPDF
function browserDetection()
{
$UA = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$SF = strstr($UA, 'Safari') ? true : false;
$OP = strstr($UA, 'Opera') ? true : false;
$OPV = $OP ? preg_split('/opera\//i', $UA) : false;
$OPV = $OPV ? floatval($OPV[1]) : false;
$FF = !$OP && strstr($UA, 'Firefox') ? true : false;
$FFV = $FF ? preg_split('/firefox\//i', $UA) : false;
$FFV = $FFV ? floatval($FFV[1]) : false;
$IE = !$OP && !$FF && strstr($UA, 'MSIE') ? true : false;
$IEV = $IE ? preg_split('/msie/i', $UA) : false;
$IEV = $IEV ? floatval($IEV[1]) : false;
if ($IE){
return true;
}else{
return false;
}
}//end function broswerDetection
function showPDF($file)
{
$name = basename($file);
header("Cache-Control: maxage=1"); //In seconds
header("Pragma: public");
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called $name.pdf
header('Content-Disposition: attachment; filename="$name"');
// The PDF source is in original.pdf
readfile("$file");
}//end function showPDF
?>
--
Bastien
Cat, the other other white meat