DOMPDF Attachment issue in IE (Internet Explorer)

dompdf_ieI have used DOMPDF in my project. But I was facing problem with Attachment. In FireFox it was working fine. But in Internet Explorer (I used IE 6) it ended with an error. In IE it was showing the download dialogue box offering the script page I used to generate the PDF file. When I tried to download the file it showed an error.

I searched the web and found no suitable solution. I followed the instruction given in http://www.corenettech.com/blog/ but it didn’t work :(. But I got an idea from this post. I started digging the code and have done following changes to PDFLib_Adapter class (available in DOMPDF_DiRECTORY/include/pdflib_adapter.cls.php).

I have removed the following line (line 829):

header("Cache-Control: private");

and added

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
     header('Expires: 0');
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header("Content-Transfer-Encoding: binary");
     header('Pragma: public');
     header("Content-Length: ".strlen($data));
}
else
{
     header("Cache-Control: private");
     header("Content-Transfer-Encoding: binary");
     header('Expires: 0');
     header('Pragma: no-cache');
     header("Content-Length: ".strlen($data));
}

After

header("Content-type: application/pdf");
header("Content-Disposition: $attach; filename=\"$filename\"");

That’s it! My code started working :).

Hope this will save your time of surfing the net ;).

Share

11 Comments

  • By Andy D, May 11, 2009 @ 8:39 pm

    I am having the same problem with IE.
    I’m using version 0.5.1
    My pdflib_adapter.cls.php looks like this starting at line 833:

    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    
        //header("Cache-Control: private");
        header("Content-type: application/pdf");
        header("Content-Disposition: $attach; filename="$filename"");
     
    	if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
    	{
    		 header('Expires: 0');
    		 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    		 header("Content-Transfer-Encoding: binary");
    		 header('Pragma: public');
    		 header("Content-Length: ".strlen($data));
    	}
    	else
    	{
    		 header("Cache-Control: private");
    		 header("Content-Transfer-Encoding: binary");
    		 header('Expires: 0');
    		 header('Pragma: no-cache');
    		 header("Content-Length: ".strlen($data));
    	}

    When I use $dompdf->stream(“sample.pdf”, array(“Attachment” => 0)); nothing happens at all, when I remove the array(“Attachment” => 0) the save file dialog pops up, but then an error appears saying that the page can’t be loaded.

    Just checking to see if I did anything wrong with your instructions.

  • By Mohammad Sajjad Hossain, May 11, 2009 @ 10:04 pm

    The change you made seems alright. Try using following code:

    $options['Attachment'] = 1;
    $options['Accept-Ranges'] = 0;
    $options['compress'] = 1;
    $dompdf->stream("sample.pdf", $options);
  • By Andy D, May 11, 2009 @ 10:29 pm

    I played around with it a little more, and part of the code I was using to create the page was an include that had session_start(); to access some session variables (such as username) in order to load the correct information.
    when I removed the session_start from the page, it fixed that problem, but it won’t give me all the correct data because it’s not loading the session data.

    Now I just have to figure out how to either make session_start() work with dompdf in IE, or figure out how to get the data I need without using it at all…

  • By Andy D, May 11, 2009 @ 10:31 pm

    Just using the pdftest.php page worked in IE, but if I put the session_start() anywhere on the page, it fails.
    Still works in FF though…

  • By Mohammad Sajjad Hossain, May 12, 2009 @ 10:19 am

    It seems your problem is session specific. Use session_start() as the first statement of the page and see what happens.

    For example:

    <?php
         session_start();
     
         //all includes
         include('some_file.php');
         /**
          * PHP codes
          */
    ?>
  • By Andy D, May 12, 2009 @ 8:19 pm

    Yeah, it still fails if session_start is the first line.
    First line, last line, same result.

    What I may have to do, if I can’t figure out a way to get around it, is to load the page, do all the work to build the html, and then put the data into a hidden form field, submit the form to the same page and check to make sure the $_post field isn’t empty, then show it with dompdf…

    Round about but at least it should work.

  • By alto, May 18, 2009 @ 8:59 pm

    Please help how do i convert pdf with multiple page, when i try to convert pdf it only gives me one page

    thanks

  • By matt, April 11, 2010 @ 6:35 pm

    this is old but still very useful. thanks!

  • By robyn, June 2, 2011 @ 1:53 pm

    I know ths is an old post but hoping for some help, did anyone find a good solution to the issue with session_start() and dompdf in IE? Or do I need to seach for a work around, any help would be much appreciated, thanks

  • By prashant dhumane, July 1, 2011 @ 12:49 pm

    nice, very useful.

  • By Schrotthandel, July 18, 2013 @ 8:47 pm

    thank very much. you help me a lot

Other Links to this Post

RSS feed for comments on this post.

Leave a comment

blog