Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';我的php生成的word文档不能打开吗?_Php_Encoding_Ms Word_Converter - Fatal编程技术网

为什么可以';我的php生成的word文档不能打开吗?

为什么可以';我的php生成的word文档不能打开吗?,php,encoding,ms-word,converter,Php,Encoding,Ms Word,Converter,在我的xampp htdocs文件夹中,我得到了两个文件:一个图像和一个php脚本。我试图创建一个带有图像的word文档。这是我使用的代码: $image = 'img.png'; $imageData = base64_encode(file_get_contents($image)); $src = 'data: '. mime_content_type($image).';base64,'.$imageData; header("Content-type: application/vnd

在我的xampp htdocs文件夹中,我得到了两个文件:一个图像和一个php脚本。我试图创建一个带有图像的word文档。这是我使用的代码:

$image = 'img.png';
$imageData = base64_encode(file_get_contents($image));
$src = 'data: '. mime_content_type($image).';base64,'.$imageData;

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 "<h1>bla</h1>";
echo "<b>My first document</b>";
echo '<img src="',$src,'">';
echo "</body>";
echo "</html>";
$image='img.png';
$imageData=base64_编码(文件获取内容($image));
$src='数据:'。mime_内容_类型($image)。”;base64,.$imageData;
标题(“内容类型:application/vnd.ms word”);
标题(“内容处置:附件;文件名=文档_name.doc”);
回声“;
回声“;
回声“;
呼应“bla”;
呼应“我的第一份文件”;
回声';
回声“;
回声“;
事实上,我的电脑上没有安装Microsoft Word,但它也可以与Libre Office一起使用。我也试过了,但没用。首先,我尝试了一个太大的图像,我认为这是造成问题的原因,但它甚至不工作与一个小的图像。文件一直在加载,但无法打开。文件大小似乎是正确的-它是52kb-因此图像似乎在文档中


但是什么会导致错误呢?如何查找和调试?

Word无法读取Html,至少在指定
.doc
扩展名的情况下不能

如果要使用最新版本的Word(自2007年以来),则应使用Docx生成器;如果要创建可从Word 2003读取的文档,则应使用doc生成器


在这方面效果也很好(但是没有得到很好的支持)

好吧–在edi9999和他很棒的库的帮助下,我能够用我的文本变量和图像创建一个docx文档

以下是我使用的代码: Javascript:

/*** importing all necessary scripts ***/
<script type="text/javascript" src="docxtemplater-master/libs/base64.js"></script>
<script type="text/javascript" src="docxtemplater-master/libs/jszip/jszip.js"></script>
<script type="text/javascript" src="docxtemplater-master/libs/jszip/jszip-load.js"></script>
<script type="text/javascript" src="docxtemplater-master/libs/jszip/jszip-inflate.js"></script>
<script type="text/javascript" src="docxtemplater-master/js/docxgen.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
/*** my JSON object with the variables for setTemplateVars() ***/
<script type="text/javascript">
            var JSON = { "articles": [
                { "title": "test-title", "first_name": "Paul", "last_name": "Alan", "phone": "555-nose", "fileName": "abc" },
                { "title": "test-title2", "first_name": "John", "last_name": "Doe", "phone": "555-abcd", "fileName": "bcd" }
            ]
            };
</script>
<script type="text/javascript">
var xhrDoc = new XMLHttpRequest();
xhrDoc.open('GET', 'Example.docx', true);
if (xhrDoc.overrideMimeType) {
    xhrDoc.overrideMimeType('text/plain; charset=x-user-defined');
}

xhrDoc.onreadystatechange = function (e) {
    if (this.readyState == 4 && this.status == 200) {
       window.docData = this.response;
    }
};
xhrDoc.send();


var xhrImage = new XMLHttpRequest();
xhrImage.open('GET', 'dog.jpg', true);
            if (xhrImage.overrideMimeType) {
                xhrImage.overrideMimeType('text/plain; charset=x-user-defined');
            }

            xhrImage.onreadystatechange = function (e) {
                if (this.readyState == 4 && this.status == 200) {
                    window.imgData = this.response;
                }
            };
            xhrImage.send();

generateDoc = function (docData) {
            var doc = new DocxGen(docData)

            doc.setTemplateVars(
                { "first_name": JSON.articles[0]["first_name"],
                    "last_name": JSON.articles[0]["last_name"],
                    "phone": JSON.articles[0]["phone"],
                    "fileName": JSON.articles[0]["fileName"]
                }
              )
            doc.applyTemplateVars()
            imageList = doc.getImageList()
            console.log(imageList);
            doc.setImage(imageList[0].path, imgData);
            doc.output()
        }

</script>
当然,内容毫无意义,但它是有效的。但仍然有一些问题(特别是edi9999,我希望您能为我回答:):

1。图像必须在同一台服务器上,并且必须使用相对路径,对吗?链接似乎不起作用。我尝试了
xhrImage.open('GET','http://link-to-an-image.com/image.jpg",对),但没有成功。是否可以使用图像的外部链接

2.GET请求后面的控制台中有一个304错误(“未修改”)。这正常吗

3.替换word文档中图像的图像是否必须具有相同的大小(或至少具有相同的纵横比),或者是否有任何选项变量可以使替换更加灵活?例如:如果我想在word文档中全幅显示图像,并获得具有不同纵横比的替换图像,是否可以保持该纵横比并增加word文档中图像的高度

4.如何使用多个图像进行替换?使用
xhrImage.open('GET','dog.jpg',true)仅打开一个图像。如何向“imageList”添加更多图像以及如何确定顺序

5.该库基于原型,通常在一个文档中同时使用两个(jQuery+prototype)框架会导致错误。但是我试着使用jQuery函数,它成功了。在一个文档中使用库和jQuery时,您是否遇到过任何问题?

找到了另一种解决方案: 使用此库:

创建带有格式化文本和图像的docx文件非常容易

以下是对我有效的代码:

require_once('PHPWord-master/Classes/PHPWord.php');
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$my_text='Hello world! I am formatted.';
$section->addText($my_text, array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
$section->addText(''); // adding some white space because the marginTop attribute of the image doesn't work
$filename="Jellyfish.jpg";
$size = getimagesize($filename);
$width="560"; //full width in a word document if image is 96dpi
$height=560/intval($size[0])*intval($size[1]);
$section->addImage(
$filename,
array(
    'width' => $width,
    'height' => $height,
    'align' => 'center'
)
);
$section->addText('blabla');
$filename="dog.jpg";
$size = getimagesize($filename);
$height=560/intval($size[0])*intval($size[1]);
$section->addImage(
$filename,
array(
    'width' => $width,
    'height' => $height,
    'align' => 'center'
)
);
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');

谢谢!但是,使用上述方法可以创建一个只包含文本的工作word文档。仅仅添加图片就造成了问题。我尝试了phpdocx,可以用文本和图像创建word文档。不幸的是,你无法在社区版的phpdocx中格式化文本,其他版本的文本非常昂贵:(是的,这是真的,phpdocx非常昂贵。如果你有一个使用JS的webapp,你可以看看我创建的库。它是一个模板(这意味着你不能从头开始创建一个单词,但需要创建一个模板),但你可以在这里查看演示:谢谢!你创建的工具似乎强大而有用。在我了解了如何安装和使用后,我会尝试一下。:)顺便问一下-如何在docx模板中使用图像?这可能吗?我将在演示页面上添加一个示例。同时,您也可以阅读github上的文档。它与qrcodes一起工作。
{phone}
Product name: {first_name}
Product reference : {last_name}
*in the middle is an image*
Blabla: {fileName}
*here is another image*
require_once('PHPWord-master/Classes/PHPWord.php');
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$my_text='Hello world! I am formatted.';
$section->addText($my_text, array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
$section->addText(''); // adding some white space because the marginTop attribute of the image doesn't work
$filename="Jellyfish.jpg";
$size = getimagesize($filename);
$width="560"; //full width in a word document if image is 96dpi
$height=560/intval($size[0])*intval($size[1]);
$section->addImage(
$filename,
array(
    'width' => $width,
    'height' => $height,
    'align' => 'center'
)
);
$section->addText('blabla');
$filename="dog.jpg";
$size = getimagesize($filename);
$height=560/intval($size[0])*intval($size[1]);
$section->addImage(
$filename,
array(
    'width' => $width,
    'height' => $height,
    'align' => 'center'
)
);
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');