在网页上显示word / pdf文件

时间:2016-12-13 05:47:08

标签: javascript php jquery html iframe

我有几个doc / docx / pdf格式的文件存储在服务器的文件夹中,它们的路径保存在数据库中。我希望从数据库中获取这些文件的路径,然后将其显示在我的网站上。

在数据库中,文件以这种格式存储

id    path
1    abc/request/file1.docx
2    abc/request/file2.pdf
3    abc/request/file3.docx

要显示我使用以下方法的文件

$a = $data->path;
$b = 'http://example.com/';
$r = $b.$a;  

<iframe src="http://docs.google.com/gview?url=<?php echo $r; ?>&embedded=true" style="width: 100%; height: 600px;" frameborder="0"></iframe>

问题

  

之前该文件已显示但突然之间没有显示

我做了以下检查以查看文件的有效性

  

1)文件格式正确且未损坏且存在于服务器文件夹中   2)控制台没有给出任何错误   3)试图在浏览器上运行http://docs.google.com/gview?url=http://example.com/abc/request/file1.docx,也没有显示文件,但网上给出的另一个示例网址正在工作

任何人都可以告诉您如何纠正错误。我也很感激,如果有人能告诉任何其他方式(使用jquery,javascript或任何可靠的方式)在网站上显示文件而不会打扰原始文件的格式

2 个答案:

答案 0 :(得分:0)

对于PDF,您可以使用ViewerJS进行渲染。

对于doc / docx,如果Google Docs Viewer不够稳定,请考虑使用Microsoft Office Viewer作为一种解决方法

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http%3A%2F%2Fieee802%2Eorg%3A80%2Fsecmail%2FdocIZSEwEqHFr%2Edoc' width='100%' height='900px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>
  

P / S:不知道为什么代码段不起作用,但你可以看看这里:https://jsfiddle.net/gcuzq343/

答案 1 :(得分:0)

你可以使用
1。谷歌文档查看器

<iframe src="http://docs.google.com/gview?url=http://example.com/my-document.doc&embedded=true"></iframe>


2。 Microsoft查看器

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http://example.com/my-document.doc.doc' width='800px' height='600px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>