?
Solved

ZEND PDF Problems with headers.

Posted on 2011-09-07
6
Medium Priority
?
964 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 111

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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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