Solved

ZEND PDF Problems with headers.

Posted on 2011-09-07
6
935 Views
Last Modified: 2012-06-21
hi!

i am trying to generate and download a PDF file via HTTP using ZEND_PDF, this action its activated by anchor (<a>) so at the end of the method i use the following:

        $pdfData = $pdf->render();
        header("Content-type: application/pdf");
        header("Content-Disposition: inline; filename=result.pdf");
       
        echo $pdfData;

i have no idea why its not working.

thanks,

Tony Villagomez
0
Comment
Question by:tonacio
  • 2
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36502155
A PDF file is a data string.  Here is how to force a download of a string.  See the code snippet, and test it here on my server
www.laprbass.com/RAY_force_download_string.php

You might find you want to render the PDF file to disk storage first, then download it.  That is a design consideration that is above my pay grade here ;-)

If this doesn't do the trick for you, please give us a few clues.  What does "not working" mean?  Any error messages or output of any sort?

Thanks and regards, ~Ray
<?php // RAY_force_download_string.php
error_reporting(E_ALL);

// REQUIRED FOR USE WITH THE PHP date() FUNCTIONS
date_default_timezone_set('America/New_York');

// A STRING TO DOWNLOAD - THIS IS DATA GENERATED INSIDE THE SCRIPT
$str = "Hello World!";

// A NAME FOR THE CLIENT TO SEE AND SAVE
$nom = 'helloworld.txt';

// USE CASE
force_download($str, $nom);



// FUNCTION TO FORCE A DOWNLOAD FROM A STRING
function force_download($filedata, $basename)
{
    if ($filedata)
    {
        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$basename");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        echo $filedata;
    }
}

Open in new window

0
 

Accepted Solution

by:
tonacio earned 0 total points
ID: 36506546
hi, i am using zend framework and its kind of confusing understanding how some stuffs has to be done to complete something. in this case, i was trying to download a file (pdf) from an action method inside my controller in zend, for some reason it wasnt working, i mean the button was refreshing the page to make the action happen but with not result, so the fix was going back to my view and add the following to my .phtml file:

<button onclick="window.location='pdf'">My Report</button>

so the my pdfAction() inside my controller its working fine, i dont know why, but now its working now,

thanks for the info anyways.
0
 

Author Comment

by:tonacio
ID: 36506816
I've requested that this question be closed as follows:

Accepted answer: 0 points for tonacio's comment http:/Q_27296706.html#36506546

for the following reason:

still trying to understand how this works, i mean its good for now its fixed.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36506817
Please tell us how you fixed it so others can learn from your research, thanks. ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question