脚本宝典收集整理的这篇文章主要介绍了将PHP文件中的HTML转换为PDF文件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我在此页面上有一个按钮“导出为PDF”..我正在尝试使用HTML2FPDF将此页面转换为pdf,但是我收到以下错误:
FPDF error: Unable to create output file: Entry Report
<?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 …
编辑:好的所以问题是我不允许公共写权限.更改此内容后,我设法创建pdf文件…问题是这个…. pdf文件是空白的…
我知道这与我传递一个PHP页面有关,这个页面在传递时可能是空的…我如何捕获html中的html内容,所以pdf输出不是空白的?
<?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; } @R_360_93@(); 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,filesize($link)); fclose($fp); $pdf->WriteHTML($strContent); $pdf->Output($name); echo ("PDF file generated successfully!"); } ?>
编辑@TIMFERRIS
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文件已创建但仍为空白
有关转换为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,'');
以上是脚本宝典为你收集整理的将PHP文件中的HTML转换为PDF文件全部内容,希望文章能够帮你解决将PHP文件中的HTML转换为PDF文件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。