Solved

ZEND PDF Problems with headers.

Posted on 2011-09-07
6
924 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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…

786 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