【问题标题】:Why fpdi output not working in Drupal 7 but working outside of D7为什么 fpdi 输出不能在 Drupal 7 中工作但在 D7 之外工作
【发布时间】:2018-09-12 16:20:06
【问题描述】:

我正在创建一个表单来处理管理 pdf 上传的文件类型。我正在使用fpdi。问题是它没有从这个代码下载$pdf->Output('D'); 任何人都可以帮助我。让它变得简单,因为我也是 Drupal 7 的新手。

我的代码与此类似。查看他们的页面here

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;

require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');

$pdf = new Fpdi();

$pageCount = $pdf->setSourceFile('Fantastic-Speaker.pdf');
$pageId = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);

$pdf->addPage();
$pdf->useImportedPage($pageId, 10, 10, 90);

$pdf->Output('D');

检查参数“D”。如果我在 D7 之外运行该代码,它就会按预期工作。那么也许 D7 会阻止它下载?请帮忙。

【问题讨论】:

  • 我猜你在 D7 的控制器中使用这个?我根本不熟悉 Drupal,但应该有任何类型的响应对象需要将 PDF 数据传输到客户端。 $pdf->Output('D'); 调用会将 HTTP 标头和 PDF 内容发送到可能由 Drupal 缓冲的输出。您应该使用$data = $pdf->Output('S'); 并将其转发给响应对象。

标签: drupal-7 fpdf fpdi


【解决方案1】:

我已通过将文件写入某个位置来修复此问题。然后发送 headers() 作为响应,它工作得很好。

$pdf->Output('F', '/path/to/the/file.pdf');

header("Content-type: application/pdf"); 
header('Content-Disposition: attachment; filename=download.pdf');
header("Content-Length: " . filesize('/path/to/the/file.pdf'));
header("Pragma: no-cache");
header("Expires: 0");
readfile('/path/to/the/file.pdf');

【讨论】:

  • 这与 $pdf->Output('D'); 几乎相同确实(没有临时文件)。所以主要问题似乎在其他地方。
猜你喜欢
  • 2014-11-26
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
相关资源
最近更新 更多