Generating word documents with PHP [duplicate]

时间秒杀一切 提交于 2019-11-28 18:24:12

See here:

http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php

To quote from the article the most common method:

Using HTTP Headers

In this method you need to format the HTML/PHP page using Word-friendly CSS and add header information to your PHP script. Make sure you don't use external style sheets since everything should be in the same file.

As a result user will be prompted to download a file. This file will not be 100% "original" Word document, but it certainly will open in MS Word application. You can use this method both for Unix and Windows environments.

<?php
  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment;Filename=document_name.doc");    
  echo "<html>";
  echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
  echo "<body>";
  echo "<b>My first document</b>";
  echo "</body>";
  echo "</html>";
?>

Generating word documents with JS:

I have created a simple open-source library that will replace tags by values.

For example Hi {name} with data={name:"John"} will be replaced by Hi John.

Here it is : https://github.com/edi9999/docxtemplater

Demo: https://docxtemplater.com/demo

OpenTBS can generate Docx documents in pure PHP, using the technique of templates.

Simple.

Yavor

As I mentioned here PHP Convert Word file to HTML without losing styling and images,

The best solution I've found so far is http://www.phplivedocx.org/ . You use it with the Zend framework. Very easy to set up and you get minimal deviation from the actual word format. It generates and converts word docs from/to html (among other formats) very well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!