I am generating PDF file dynamically in PHP and I have to output that PDF file in my android app I am developing. I have to output and display that PDF file. The problem is that I don't know how to send that PDF file from the PHP to the android app and how to make the android app open the PDF?
There are only two ways to output a PDF in a browser; inline and attachment.
In PHP set your content disposition, filename, and content type:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=myfile.pdf");
or
header("Content-disposition: inline; filename=myfile.pdf");
then echo the contents of the pdf as a string.
In your app, if you have a broswer window pointed to the url, the file should open up in whatever you've set your default PDF viewer to be. Or if you're using the inline disposition, it may open up right in the application.
Related
I have a web app in which some data is being exported and imported through CSV files. The users don't really have any meaning from opening them directly, so I'm forcing the browser to download the file, so they can save or forward the data to someone else.
I am using the following headers (in PHP):
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"name.csv\"");
This seems to work on PC's and phones (Android and Windows phone), but it doesn't work on iPads. iPad always shows the contents, and then doesn't offer any forward or share options (except opening or sharing the link to the page, which wouldn't work because you'd have to be logged in).
Can I force iPad to download it, or at least offer the option to attach the CSV file in a new email?
This used to work on my old hosting service. I just migrated to websynthesis and now when I download pdf files through browser, the html code for the download page is added to the pdf. The pdf file generated is ok when I download it directly, but not when downloaded through a browser. I am using the standard download script:
session_start();
header("Content-disposition: attachment; filename=$_SESSION[myfile.pdf]");
header("Content-type: application/pdf");
readfile($_SESSION[download-file.pdf]);
I have tried using many additional or alternate header statements with no success. Is there some additional header info I need to add?
Also filed support ticket with websynthesis
I am using joomla 2.5 with ROXBOX plugin and using this showing the PDF's in lightbox. I am facing problem when user configured Firefox auto download PDF files.
When Firefox configured as save PDF instead of open it in browser the light box stays blank and file started download. As we can not have control on browser, is there any way show any message when Firefox auto download for PDF is enabled?
Please Help!!
I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline. and 'Content-type to application/pdf.
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
You can use PHP to set header as shown above.
Hey guys I was wondering how I could download a file that is generated on the fly by PHP. The file I want to download would be an XML file. At the moment all my code does is create a long string with all of the data that is to put in the file, it then simply writes the string to a file and saves it with a .XML extension. This is currently working in my local machine using a copy of the website, it won't work on the web server though due to read/write permissions.
So is there a way to generate a file in the fly to be immediately downloaded without storing it on the web server?
If the same script is generating the file, you could echo or print the contents on to the page and use header to force download.
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
echo $XMLString;
?>
And if you have a different file for downloading the file, just use file_get_contents and output the file data!
That should do you :)
Just give these two things on the top of the document:
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="myxml.xml"');
?>
I have a code which force download a pdf file. Code below
$file_name = 'Bv_Ebook.pdf';
$file_url = 'http://' .$_SERVER['HTTP_HOST'].'/sites/default/files/'. $file_name;
header('Content-Type: text/html');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
This code works on our beta server and the pdf is downloaded and can be opened.
On our live server same code downloads the pdf but pdf file cannot be opened, it says:
Unable to open document
File type plain text document (text/plain) is not supported
I have gone though force download pdf and apache headers setting but nothing seems to work.
Any idea what could be the issue?
Check file permission using is_readable() to check if the file is accessible or not. If accessible then check below :
Can you please try to modify your headers to the following:
//We'll be outputting a PDF.
header('Content-type: application/pdf');
//PDF name.
header('Content-Disposition: attachment; filename="'.$file_name.'"');
Then do a file read or what ever, in my case I have used file_get_contents(file_path) + echo to display the content and worked as a charm.