谷歌云存储显示pdf与PHP

时间:2018-01-05 16:06:48

标签: php iframe google-cloud-storage

我试图在iframe中使用php显示pdf,但文件已下载。

客户代码:

 <iframe style="height: 700px;width: 80%;margin-left: 100px;" src="<?php echo $publicUrl;?>#zoom=100"></iframe>

后端使用php:

App::uses('APIController', 'Controller');
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
$publicFileText = sprintf('new file written at %s', date('Y-m-d H:i:s'));

$options = ['gs' => ['acl' => 'public-read']];
$context = stream_context_create($options);
$fileName = "gs://{Project_ID}.appspot.com/test/test.pdf";
file_put_contents($fileName, $publicFileText, 0, $context);
$publicUrl = CloudStorageTools::getPublicUrl($fileName, false);

如何在iframe中显示pdf文件?

2 个答案:

答案 0 :(得分:0)

您需要在浏览器中安装pdf viewer附加组件。您可以在下方查看更多详情

https://helpx.adobe.com/acrobat/using/display-pdf-in-browser.html

答案 1 :(得分:0)

要在浏览器中显示PDF,服务器必须返回两个标题:

  1. Content-Type: application/pdf告诉浏览器该文件是PDF。
  2. Content-Disposition: inline告诉浏览器显示PDF而不是保存它。
  3. 您可以使用header()函数在PHP中设置标题,但必须在发送任何其他内容之前执行此操作。