将PHP文件中的HTML转换为PDF文件 [英] Converting HTML in PHP File to PDF File
问题描述
我有一个php文件查询数据库,然后相应地写入和更新html。
我在这个页面上有一个按钮Export As PDF..我试图用HTML2FPDF将这个页面转换为pdf,但是我得到以下错误:
FPDF错误:无法创建输出文件:Entry Report
我怀疑问题是因为我传递了一个php文件而不是html文件:
<?php
require('html2fpdf.php');
if(isset($ _ POST ['link'])){
$ url = $ _POST ['link'];
$ nm = $ _POST ['nm'];
convert($ url,$ nm);
函数转换($ link,$ name){
$ pdf = new HTML2FPDF();
$ pdf-> AddPage();
$ fp = fopen($ link,r);
$ strContent = fread($ fp,filesize($ link));
fclose($ fp);
$ pdf-> WriteHTML($ strContent);
$ pdf->输出($ name);
echo(PDF文件生成成功!);
}
?>
有谁知道我该如何解决这个问题?也许通过将php转换为html然后将html转换为pdf ...或者是否有直接的方法将php文件转换为pdf文件...
>编辑:好,所以问题是我不允许公共写权限。在改变这个之后,我设法创建了pdf文件......问题是这个...... pdf文件是空白的......
我知道这样做这与我传递一个可能是空的PHP页面的事实有关......如何在HTML中捕获html内容,以便PDF输出不是空白的?
以下是php页面:
<?php
函数connect(){
$ dbh = mysql_connect(localhost,user,password)或die('我无法连接到数据库,因为:'。mysql_error());
mysql_select_db(PDS,$ dbh);
返回$ dbh;
}
session_start();
$ b $ if(isset($ _ SESSION ['username'])){
if(isset($ _ POST ['entryId'])){
//做某事
$ dbh = connect();
$ ide = $ _POST ['entryId'];
$ usertab = $ _POST ['usertable'];
$ answertable = $ usertab。 答案;
$ entrytable = $ usertab。 条目;
$ query = mysql_query(SELECT e.date,q.questionNumber,q.question,q.sectionId,a.answer FROM $ answertable a,Questions q,$ entrytable e WHERE a.entryId ='$ ide' AND a.questionId = q.questionId AND e.entryId ='$ ide'ORDER BY q.questionNumber ASC;)or die(Error:。mysql_error());
if($ query){
//设置变量
$ sectionOne = array();
$ sectionTwo = array();
$ sectionThree = array();
$ sectionFour = array();
$ sectionFive = array();
while($ row = mysql_fetch_assoc($ query)){
$ date = $ row ['date'];
$ section = $ row ['sectionId'];
switch($ section){
case'1':
$ sectionOne [] = $ row;
休息;
case'2':
$ sectionTwo [] = $ row;
休息;
案例'3':
$ sectionThree [] = $ row;
休息;
case'4':
$ sectionFour [] = $ row;
休息;
case'5':
$ sectionFive [] = $ row;
休息;
默认值:
break;
}
}
} else {
//错误 - sql失败
}
}
?>
< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =enlang =en>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = iso-8859-1/>
< script src =jQuery.js>< / script>
< script>
$(document).ready(function(){
$(#export)。click(function(e){
// post to html2pdfconverter.php
$ (#link)。val(Testers / viewentryreport.php);
$(#nm)。val(Entry Report.pdf);
$(form#sendanswers ).submit();
});
});
< / script>
< title>个人日记系统 - 输入报告 - <?php echo($ date); ?>< /标题>
< / head>
< body>
< h1>条目报告 - <?php echo($ date); ?>< / H1>
< div id =buttons>
< form id =sendanswersname =sendanswersaction =Testers / html2pdfconverter.phpmethod =post>
< input type =hiddenname =linkid =linkvalue =>
< input type =hiddenname =nmid =nmvalue =>
< input type =buttonname =exportid =exportvalue =Export As PDF/>
< / form>
< / div>
< h3>生物信息< / h3>
<?php
$ i = 0;
foreach($ sectionOne as& $ value){
if($ i == 1 || $ i == 3){
$ image =assets / urine。$ i 。 PNG;
echo(< br />);
echo($ value ['question']。< br />。< img src = \$ image \/>);
echo(< br />);
} else {
echo($ value ['question']。':'。$ value ['answer']);
}
echo(< br />);
$ i ++;
}
?>
< h3>疲劳和恢复< / h3>
<?php
foreach($ sectionTwo as& $ value){
echo($ value ['question']。':'。$ value ['answer']);
echo(< br />);
}
?>
< h3>疾病和伤害< / h3>
<?php
foreach($ sectionThree as& $ value){
echo($ value ['question']。':'。$ value ['answer']);
echo(< br />);
}
?>
< h3>培训会话< / h3>
<?php
foreach($ sectionFour as& $ value){
echo($ value ['question']。':'。$ value ['answer']);
echo(< br />);
}
?>
< h3>一般反馈< / h3>
<?php
if(count($ sectionFive)> 0){
foreach($ sectionFive as& $ value){
echo($ value ['question ']。':'。$ value ['answer']);
}
} else {
$ sectionFive =用户没有留下任何反馈;
echo(用户没有留下任何反馈);
}
echo(< br />);
?>
< / body>
< / html>
<?php
}
?> b
和html2pdfconverter.php > <?php
require('html2fpdf.php');
if(isset($ _ POST ['link'])){
$ url = $ _POST ['link'];
$ nm = $ _POST ['nm'];
convert($ url,$ nm);
函数转换($ link,$ name){
$ pdf = new HTML2FPDF();
$ pdf-> AddPage();
$ fp = fopen($ link,r);
$ strContent = fread($ fp,filesize($ link));
fclose($ fp);
$ pdf-> WriteHTML($ strContent);
$ pdf->输出($ name);
echo(PDF文件生成成功!);
}
?>
编辑@TIMFERRIS
将函数转换为基于@TimeFerris的答案:
函数转换($ link,$ name){
$ pdf = new HTML2FPDF();
$ pdf-> AddPage();
$ content = file_get_contents($ link); //比你所做的更短
ob_start();
echo $ content;
$ pdf-> WriteHTML(ob_get_contents());
ob_end_clean();
$ pdf->输出($ name);
echo(PDF文件生成成功!);
}
PDF文件已创建,但仍为空白
您提供的php文件必须在解析器运行有效的html代码之前运行。
$ content = file_get_contents($ filename); //比你所做的更短
ob_start();
echo $ content;
$ pdf-> WriteHTML(ob_get_contents());
ob_end_clean();
I have a php file which queries a database and then writes and updates the html accordingly.
I have a button on this page "Export As PDF"..I am trying to convert this page to a pdf using HTML2FPDF, but I get the following error:
FPDF error: Unable to create output file: Entry Report
I suspect the problem is because I am passing a php file instead of a html file:
<?php
require('html2fpdf.php');
if(isset($_POST['link'])){
$url = $_POST['link'];
$nm = $_POST['nm'];
convert($url, $nm);
}
function convert($link, $name){
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen($link,"r");
$strContent = fread($fp, filesize($link));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output($name);
echo ("PDF file generated successfully!");
}
?>
Does anyone know how I can get around this? perhaps by either converting php to html then html to pdf...
Or is there a direct way to convert a php file into a pdf file...
EDIT: OK so the problem was that I didn't allow public write permissions. Upon changing this, I managed it to create the pdf file...The problem is this....the pdf file is blank...
I know this has something to do with the fact that I am passing a php page which is probably empty when it is being passed...How do I capture the html content within the html so the pdf output isn't blank?
Here is the php page:
<?php
function connect() {
$dbh = mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
session_start();
if(isset($_SESSION['username'])){
if(isset($_POST['entryId'])){
//do something
$dbh = connect();
$ide = $_POST['entryId'];
$usertab = $_POST['usertable'];
$answertable = $usertab . "Answers";
$entrytable = $usertab . "Entries";
$query = mysql_query("SELECT e.date, q.questionNumber, q.question, q.sectionId, a.answer FROM $answertable a, Questions q, $entrytable e WHERE a.entryId = '$ide' AND a.questionId = q.questionId AND e.entryId = '$ide' ORDER BY q.questionNumber ASC;") or die("Error: " . mysql_error());
if($query){
//set variables
$sectionOne = array();
$sectionTwo = array();
$sectionThree = array();
$sectionFour = array();
$sectionFive = array();
while($row=mysql_fetch_assoc($query)){
$date = $row['date'];
$section = $row['sectionId'];
switch($section){
case '1':
$sectionOne[] = $row;
break;
case '2':
$sectionTwo[] = $row;
break;
case '3':
$sectionThree[] = $row;
break;
case '4':
$sectionFour[] = $row;
break;
case '5':
$sectionFive[] = $row;
break;
default:
break;
}
}
}else{
//error - sql failed
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src = "jQuery.js"></script>
<script>
$(document).ready(function(){
$("#export").click(function(e){
//post to html2pdfconverter.php
$("#link").val("Testers/viewentryreport.php");
$("#nm").val("Entry Report.pdf");
$("form#sendanswers").submit();
});
});
</script>
<title>Personal Diary System - Entry Report - <?php echo($date); ?></title>
</head>
<body>
<h1>Entry Report - <?php echo($date); ?></h1>
<div id = "buttons">
<form id = "sendanswers" name = "sendanswers" action="Testers/html2pdfconverter.php" method="post">
<input type = "hidden" name = "link" id = "link" value = "">
<input type = "hidden" name = "nm" id = "nm" value = "">
<input type = "button" name = "export" id = "export" value = "Export As PDF"/>
</form>
</div>
<h3>Biological Information</h3>
<?php
$i = 0;
foreach($sectionOne as &$value){
if($i == 1 || $i == 3){
$image = "assets/urine".$i.".png";
echo("<br/>");
echo($value['question']." <br/> "."<img src = \"$image\"/>");
echo("<br/>");
}else{
echo($value['question'].' : '.$value['answer']);
}
echo("<br/>");
$i++;
}
?>
<h3>Fatigue and Recovery</h3>
<?php
foreach($sectionTwo as &$value){
echo($value['question'].' : '.$value['answer']);
echo("<br/>");
}
?>
<h3>Illness and Injury</h3>
<?php
foreach($sectionThree as &$value){
echo($value['question'].' : '.$value['answer']);
echo("<br/>");
}
?>
<h3>Training Sessions</h3>
<?php
foreach($sectionFour as &$value){
echo($value['question'].' : '.$value['answer']);
echo("<br/>");
}
?>
<h3>General Feedback</h3>
<?php
if(count($sectionFive)>0){
foreach($sectionFive as &$value){
echo($value['question'].' : '.$value['answer']);
}
}else{
$sectionFive = "User didn't leave any feedback";
echo("User didn't leave any feedback");
}
echo("<br/>");
?>
</body>
</html>
<?php
}
?>
and html2pdfconverter.php
<?php
require('html2fpdf.php');
if(isset($_POST['link'])){
$url = $_POST['link'];
$nm = $_POST['nm'];
convert($url, $nm);
}
function convert($link, $name){
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen($link,"r");
$strContent = fread($fp, filesize($link));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output($name);
echo ("PDF file generated successfully!");
}
?>
EDIT @TIMFERRIS
I changed my convert function to this based on @TimeFerris' answer:
function convert($link, $name){
$pdf=new HTML2FPDF();
$pdf->AddPage();
$content = file_get_contents( $link ); //shorter than what you did
ob_start();
echo $content;
$pdf->WriteHTML( ob_get_contents() );
ob_end_clean();
$pdf->Output($name);
echo ("PDF file generated successfully!");
}
The PDF file is created but still blank
The php file you are supplying has to run through a parser before it's valid html code.
$content = file_get_contents( $filename ); //shorter than what you did
ob_start();
echo $content;
$pdf->WriteHTML( ob_get_contents() );
ob_end_clean();
这篇关于将PHP文件中的HTML转换为PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!