Jump to content

View a pdf file faster in browser


tkm

Recommended Posts

Hello Mates,

  I am using the following code to download a pdf file of size 3MB.

 

function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;

} 

$file="test.pdf";
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Type: application/pdf");

header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
ob_end_flush();
set_time_limit(0);
//readfile($file);
readfile_chunked($file);
exit;

I put in all available suggestions I got in order to make it faster and hence all the above extra code. But still it is taking too much time to show the pdf on browser. Any suggestion would be great help. Thank you.

 

 

Link to comment
https://forums.phpfreaks.com/topic/107827-view-a-pdf-file-faster-in-browser/
Share on other sites

how fast is fast enough? the slow part is going to be moving a 3MB across the internet from your server to your browser. i get about 10MB/minute download here, so that's about 18 seconds or so for me to download your PDF. If that's too slow, you'll have to buy me faster internet.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.