我们可以使用php中的文件处理来编写和打开pdf文件吗


can we write and open pdf file using file handling in php

我使用php中的文件处理来创建写然后读pdf文件。问题是它创建了pdf文件,但无法打开,当我打开文件时,它只是说

"Adobe阅读器无法打开此文件,因为它不是受支持的文件类型,或者文件已损坏(例如:它是作为电子邮件附件发送的,没有正确解码)"

我的代码如下:

<?php
$file = "testFile.pdf";
$fh = fopen($file, 'w') or die("can't open file");
$stringData = "Bobby Bopper'n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner'n";
fwrite($fh, $stringData);
header("Cache-control: private");
header("Content-Type: application/pdf");
header("Content-Length: ".filesize($file));
header("Content-Disposition: inline; filename=$file");
readfile($file);
fclose ($fd);
?>

您没有创建PDF文件。你正在创建一个文本文件,并将.pdf作为扩展名,这将不起作用。

一个简单的解决方案是使用dompdf。