将PHP文件中的HTML转换为PDF文件

前端之家收集整理的这篇文章主要介绍了将PHP文件中的HTML转换为PDF文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个查询数据库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,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文件已创建但仍为空白

我最近使用 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,'');

猜你在找的PHP相关文章