Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting HTML in PHP File to PDF File
    text
    copied!<p>I have a php file which queries a database and then writes and updates the html accordingly. </p> <p>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:</p> <pre><code>FPDF error: Unable to create output file: Entry Report </code></pre> <p>I suspect the problem is because I am passing a php file instead of a html file:</p> <pre><code>&lt;?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-&gt;AddPage(); $fp = fopen($link,"r"); $strContent = fread($fp, filesize($link)); fclose($fp); $pdf-&gt;WriteHTML($strContent); $pdf-&gt;Output($name); echo ("PDF file generated successfully!"); } ?&gt; </code></pre> <p>Does anyone know how I can get around this? perhaps by either converting php to html then html to pdf...</p> <p>Or is there a direct way to convert a php file into a pdf file...</p> <p>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...</p> <p>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?</p> <p>Here is the php page:</p> <pre><code>&lt;?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 } } ?&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt; &lt;script src = "jQuery.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function(){ $("#export").click(function(e){ //post to html2pdfconverter.php $("#link").val("Testers/viewentryreport.php"); $("#nm").val("Entry Report.pdf"); $("form#sendanswers").submit(); }); }); &lt;/script&gt; &lt;title&gt;Personal Diary System - Entry Report - &lt;?php echo($date); ?&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Entry Report - &lt;?php echo($date); ?&gt;&lt;/h1&gt; &lt;div id = "buttons"&gt; &lt;form id = "sendanswers" name = "sendanswers" action="Testers/html2pdfconverter.php" method="post"&gt; &lt;input type = "hidden" name = "link" id = "link" value = ""&gt; &lt;input type = "hidden" name = "nm" id = "nm" value = ""&gt; &lt;input type = "button" name = "export" id = "export" value = "Export As PDF"/&gt; &lt;/form&gt; &lt;/div&gt; &lt;h3&gt;Biological Information&lt;/h3&gt; &lt;?php $i = 0; foreach($sectionOne as &amp;$value){ if($i == 1 || $i == 3){ $image = "assets/urine".$i.".png"; echo("&lt;br/&gt;"); echo($value['question']." &lt;br/&gt; "."&lt;img src = \"$image\"/&gt;"); echo("&lt;br/&gt;"); }else{ echo($value['question'].' : '.$value['answer']); } echo("&lt;br/&gt;"); $i++; } ?&gt; &lt;h3&gt;Fatigue and Recovery&lt;/h3&gt; &lt;?php foreach($sectionTwo as &amp;$value){ echo($value['question'].' : '.$value['answer']); echo("&lt;br/&gt;"); } ?&gt; &lt;h3&gt;Illness and Injury&lt;/h3&gt; &lt;?php foreach($sectionThree as &amp;$value){ echo($value['question'].' : '.$value['answer']); echo("&lt;br/&gt;"); } ?&gt; &lt;h3&gt;Training Sessions&lt;/h3&gt; &lt;?php foreach($sectionFour as &amp;$value){ echo($value['question'].' : '.$value['answer']); echo("&lt;br/&gt;"); } ?&gt; &lt;h3&gt;General Feedback&lt;/h3&gt; &lt;?php if(count($sectionFive)&gt;0){ foreach($sectionFive as &amp;$value){ echo($value['question'].' : '.$value['answer']); } }else{ $sectionFive = "User didn't leave any feedback"; echo("User didn't leave any feedback"); } echo("&lt;br/&gt;"); ?&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php } ?&gt; </code></pre> <p>and html2pdfconverter.php</p> <pre><code>&lt;?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-&gt;AddPage(); $fp = fopen($link,"r"); $strContent = fread($fp, filesize($link)); fclose($fp); $pdf-&gt;WriteHTML($strContent); $pdf-&gt;Output($name); echo ("PDF file generated successfully!"); } ?&gt; </code></pre> <p>EDIT @TIMFERRIS</p> <p>I changed my convert function to this based on @TimeFerris' answer:</p> <pre><code>function convert($link, $name){ $pdf=new HTML2FPDF(); $pdf-&gt;AddPage(); $content = file_get_contents( $link ); //shorter than what you did ob_start(); echo $content; $pdf-&gt;WriteHTML( ob_get_contents() ); ob_end_clean(); $pdf-&gt;Output($name); echo ("PDF file generated successfully!"); } </code></pre> <p>The PDF file is created but still blank</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload