我有一个查询数据库的php文件,然后相应地写入和更新html。
我在此页面上有一个按钮“导出为PDF”..我正在尝试使用HTML2FPDF将此页面转换为pdf,但是我收到以下错误:
FPDF error: Unable to create output file: Entry Report
我怀疑问题是因为我传递的是php文件而不是html文件:
<?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!");
}
?>
有谁知道我怎么能解决这个问题?也许通过将php转换为html然后将html转换为pdf ...
或者是否有直接的方法将php文件转换为pdf文件...
编辑:好的,问题是我不允许公共写权限。更改此内容后,我设法创建了pdf文件......问题是这个.... pdf文件是空白的......
我知道这与我传递一个php页面有关,这个页面在传递时可能是空的...我如何捕获html中的html内容,以便pdf输出不是空白?
这是php页面:
<?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
}
?>
和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!");
}
?>
编辑@TIMFERRIS
我根据@TimeFerris的答案改变了我的转换函数:
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!");
}
PDF文件已创建但仍为空白
答案 0 :(得分:1)
我最近使用TCPDF完成了此操作。 TCPDF是FPDF的进一步开发版本。
有关转换为PDF的示例,请参阅this link。另外,here您可以找到更多示例。例61也可以派上用场。
TCPDF使用的示例代码:
$html = '<h1>HTML Example</h1>
<div style="text-align:center">IMAGES<br />
<img src="../images/logo_example.png" alt="test alt attribute" width="100" height="100" border="0" /><img src="../images/tiger.ai" alt="test alt attribute" width="100" height="100" border="0" /><img src="../images/logo_example.jpg" alt="test alt attribute" width="100" height="100" border="0" />
</div>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
答案 1 :(得分:1)
您可以做的一件事是运行PHP文件,但使用输出缓冲来捕获PHP脚本正在写出的内容。然后,将这些内容保存到.html文件并将该文件传递给转换器。
答案 2 :(得分:1)
您提供的php文件必须在有效的html代码之前运行解析器。
$content = file_get_contents( $filename ); //shorter than what you did
ob_start();
echo $content;
$pdf->WriteHTML( ob_get_contents() );
ob_end_clean();