php 如何生成pdf并保存到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12066875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:44:25  来源:igfitidea点击:

how to generate pdf and save to server

phppdf

提问by PHP Coder

Well, i'm trying to generate pdf file and save it to server but when i'm trying to view that pdf its showing invalid format.

好吧,我正在尝试生成 pdf 文件并将其保存到服务器,但是当我尝试查看该 pdf 时,它显示的格式无效。

Code :

代码 :

<?php
$content = "32w434";
file_put_contents("xyz.pdf",$content);
?>

If i change file from pdf to doc it works and opening perfectly but not pdf

如果我将文件从 pdf 更改为 doc 它可以正常工作并完美打开但不是 pdf

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output();
file_put_contents("xyz.pdf",$content);

That code giving output i don't want output on browser and still file is corrupted / invalid format

给出输出的代码我不希望在浏览器上输出并且仍然文件已损坏/格式无效

回答by Mohit Bumb

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output('doc.pdf','F');

http://www.fpdf.org/

http://www.fpdf.org/

回答by Suresh kumar

You can use FPDF to generate pdf file....

您可以使用 FPDF 生成 pdf 文件....

http://www.fpdf.org/

This is simple and best way to generate pdf...

这是生成pdf的简单且最好的方法...

回答by Muthu Kumaran

Use TCPDF to generate PDF file,

使用TCPDF生成PDF文件,

TCPDF Site: http://www.tcpdf.org/

TCPDF 网站:http://www.tcpdf.org/

Demo: http://www.tcpdf.org/examples.php

演示:http: //www.tcpdf.org/examples.php

回答by alee

@Mohit,

@莫希特,

This

这个

$content = $pdf->Output('doc.pdf','F');

generates a new file. If your file is generated using variables that can change in php, the pdf file you see on screen, will be different from the pdf file saved on the server.

生成一个新文件。如果您的文件是使用可以在 php 中更改的变量生成的,则您在屏幕上看到的 pdf 文件将与保存在服务器上的 pdf 文件不同。

回答by dhchen

You should generate pdf file using pdf libraries such as tcpdf or fpdf. Putting contents directly into a file with .pdf extension will definitely not working since pdf has its own file format

您应该使用 pdf 库(例如 tcpdf 或 fpdf)生成 pdf 文件。将内容直接放入带有 .pdf 扩展名的文件中肯定不起作用,因为 pdf 有自己的文件格式