Solved

ZEND PDF Problems with headers.

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

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now