Php header doc file

Create a DOC file with PHP on the fly

Ever wanted to save a page to a word doc file? this tutorial shows you just how easy it is to do.

I needed to save a page that is dynamically driven from a database and save it’s contents to a doc file so I search Google, I saw lots of articles talking about com classes and third party extensions and applications/api’s for something this light it should need all that. and it doesn’t it turns out using the very super friendly header tag that’s built into php will do the job perfectly.

Set 2 headers using Content-type and set to application/vnd.ms-word and the second Content-Disposition creates the attachment then you can name the file anything you like followed by .doc in this case yourcoolwordfile.doc.

Note, make sure there is nothing being shown on screen before the headers or you will produce errors.

Then start your normal html markup

So the page is created correctly place the following meta tag in your header markup

Then inside the body tags put what you would like to be displayed in the doc file such as:

Your very cool web page in Word!

This text could quite easily from directly from a database. oh the possibilities.

This is a very simple example of creating a doc file with php.

That’s all there is to it. Here’s a sample script:

      

Your very cool web page in Word!

This text could quite easily from directly from a database. oh the possibilities.

This is a very simple example of creating a doc file with php.

Источник

Create a DOC file with PHP on the fly

Ever wanted to save a page to a word doc file? this tutorial shows you just how easy it is to do.

I needed to save a page that is dynamically driven from a database and save it’s contents to a doc file so I search Google, I saw lots of articles talking about com classes and third party extensions and applications/api’s for something this light it should need all that. and it doesn’t it turns out using the very super friendly header tag that’s built into php will do the job perfectly.

Set 2 headers using Content-type and set to application/vnd.ms-word and the second Content-Disposition creates the attachment then you can name the file anything you like followed by .doc in this case yourcoolwordfile.doc.

Note, make sure there is nothing being shown on screen before the headers or you will produce errors.

Then start your normal html markup

So the page is created correctly place the following meta tag in your header markup

Then inside the body tags put what you would like to be displayed in the doc file such as:

Your very cool web page in Word!

This text could quite easily from directly from a database. oh the possibilities.

This is a very simple example of creating a doc file with php.

That’s all there is to it. Here’s a sample script:

      

Your very cool web page in Word!

This text could quite easily from directly from a database. oh the possibilities.

This is a very simple example of creating a doc file with php.

Источник

PHP — What are the correct headers for downloading file?

I have a piece of code that allow users download file from server (document such as docs,docx,pdf etc). Users can download files but it has some errors like the files were broken. For example, a MS Word file after download need to recovery to read content. I wonder that if there is any mistake in this code (or problem when uploading?).

$size_of_file = filesize($download_path); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $file_name); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $size_of_file); //read file from physical path readfile($download_path); 

enter image description here

You could try generating a content-type depending on the file type, instead of using always «octet-stream». Another comment: Try removing the «file transfer» header. I have done what you want in several projects and I have never user such a header.

does your readfile() method print the contents of your files? you should write them with the echo() command.

@belgther — readfile() always print file content to stdout. Back to question, maby you print something (space, newline etc) before readfile and it breaks downloaded file? It’s possible because file is ok after recovery.

Источник

PHP Create Word Document from HTML

In this article, you will learn how to create a word document in doc or docx using the PHP programming language.

The conversion of HTML into MS Word Document is primarily utilised in the web application to produce .doc/.docx records with dynamic HTML content. The server-side script is needed to dynamically export data from HTML to Word Document. The MS Word document can be quickly generated with HTML content using a PHP script. There are many third-party libraries available for HTML to Word conversion. But, you can easily convert the HTML content to a Word document using PHP without any external library.

PHP provides features for adding HTTP headers. Like, we can set the HTTP header to download the content or attachment, and we can also set it to show a ‘Save as‘ dialog box while downloading on the browser. So in this example, we have added the content headers to make the generated doc file downloadable. For file formatting, we have utilized word-accommodating CSS. It is important to use inline CSS instead of an external CSS file.

Create And Download Word Document in PHP

Here is the script to generate and download a word document. So, copy and paste this code either on the localhost or on the server, only you will have to change the body element content.

 $filename = 'demo.doc'; header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename color: #0000ff;">$filename)); header( "Content-Description: File Transfer"); @readfile($filename); $content = ' xmlns:v="urn:schemas-microsoft-com:vml" ' .'xmlns:o="urn:schemas-microsoft-com:office:office" ' .'xmlns:w="urn:schemas-microsoft-com:office:word" ' .'xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"= ' .'xmlns="http://www.w3.org/TR/REC-html40">' .' http-equiv="Content-Type" content="text/html; charset=Windows-1252">' .' ' .'
Читайте также:  font-weight
Оцените статью