Actually I want to convert html into word(.doc). I am using the following code to convert html to word
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
But This code is requesting force download in client system. How can i write the file in server as Word DoCument. Please Help me to resolve my problem.
Related
I'm trying to display a preview of a embedded word document using Office's live view. I do not want to use a saved .docx file. I'm using a PHPWORD library to create the document using it's templateProccessing class.
Everything works when i'm referring to a path of a saved document but not as a php output.
What i have so far is
HTML
<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=https://homeurl.com/panel/export?code=MYaM14lMfqm8NFYxpO67&id=43' ' width='1366px' height='623px' frameborder='0'></iframe>
And on the server side i have:
PHP
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=" . $params['document_name']. ".docx");
header("Content-Transfer-Encoding: binary");
$templateProcessor->saveAs("php://output");
The iframe preview says that it's unable to read the file.
I think it has something to do with the header information but i haven't found yet a solution for this.
I found out that it doesn't work if file url doesn't contain filename and extension, just like in your case. Somehow it works if you modify your url:
<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=https://homeurl.com/panel/export/file.docx?code=MYaM14lMfqm8NFYxpO67&id=43' ' width='1366px' height='623px' frameborder='0'></iframe>
Can't find any documentation to find out if there is a better way to make it work.
I'm creating MS Office Word document with PHP as following:
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=filename.doc");
header("Content-Transfer-Encoding: binary");
But I need to save file to the server without save as dialog to be able to do something with the created *.doc file and give it to user only after the modification.
So how can I acheive this?
The code you use is for sending as download to the browser user.
To save on the server, use the this in PHP :
file_put_contents('path/to/ms/word/document.doc', $theData);
Then after you use your code to sent the saved file.
I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.
I am using the following code in a php document to force download of a pdf form since the submission works only after you have it on your local machine rather online.
It downloads the file ok but it corrupts it.
I can no longer open the pdf document.
<?php
$file_name = 'costumer.pdf';
$file_url = 'http://www.lopezi.com/forms/' . $file_name;
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
?>
The Content-Transfer-Encoding header shouldn't be needed in this case. Further I suspect that you have corruption in the outputted file.
Download it somewhere, open notepad, and drag the file in there. If any PHP warnings or errors were generated you will see them at the top.
Also, try to avoid the option of having more content return from the script, causing problems with the download, end with something like:
die(file_get_contents($file_url));
This way you cannot accidentally break the code easily by adding more output.
I am creating an xml file on the fly. When a user generates this file I want it to open up a download file dialog with the content that was generated. There is no actual file because it is just generated through php. Any ideas on how to do this?
This is what worked for me. In readfile('newfile.xml'); make sure to give the path of the file correctly. This php page is called from an html page with anchor tag which says - download:
<?php
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('newfile.xml');
?>
source: How do I force the browser to download a file to disk instead of playing or displaying it?
Send a content-disposition attachment header.
header('Content-disposition: attachment; filename=advertise.xml');
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
// if you want to directly download then set expires time
header("Expires: 0");