用于将HTML导出为pdf的工具 [英] Tool for exporting html as pdf

查看:117
本文介绍了用于将HTML导出为pdf的工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html文档标记一个报告。我在这个页面上有一个按钮Export as pdf。但是我不知道如何将html导出到pdf中。任何人都建议使用任何工具。



编辑:更详细地说:



我有以下php:

 <?php 

函数connect(){
$ dbh = mysql_connect(localhost,user,password)或死('我无法连接到数据库,因为''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'];
$ answersertable = $ usertab。 答案;
$ entrytable = $ usertab。 条目;
$ query = mysql_query(SELECT e.date,q.questionNumber,q.question,q.sectionId,a.answer FROM $ answersertable a,Questions q,$ entrytable e WHERE a.entryId ='$ ide' AND a.questionId = q.questionId AND e.entryId ='$ ide'ORDER BY q.questionNumber ASC;)或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;
break;
case'2':
$ sectionTwo [] = $ row;
break;
case'3':
$ sectionThree [] = $ row;
break;
case'4':
$ sectionFour [] = $ row;
break;
case'5':
$ sectionFive [] = $ row;
break;
默认值:
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(){

});
< / script>
< title>个人日记系统 - 输入报告 - <?php echo($ date); ?>< /标题>
< / head>
< body>
< h1>条目报告 - <?php echo($ date); ?>< / H1>
< div id =buttons>
< a href =http://dev.speechlink.co.uk>导出为PDF< / a>
< / 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 {
echo(用户没有留下任何反馈);
}
echo(< br />);
?>
< / body>
< / html>
<?php
}
?>

显示以下内容:



< img src =https://i.stack.imgur.com/24I8J.pngalt =enter image description here>



所以如果我在使用fpdf,以pdf格式导出以下的最好方法是什么?我应该在同一个php文件中编写一个fpdf函数,还是最好编写一个单独的php文件来创建和显示pdf(这意味着我必须将所有相关数据发布到这个文件)...

解决方案

使用FPDF库进行php
查看此处



此文件转换的第一个和主要基础是FPDF库。 FPDF是一个纯PHP类,用于生成PDF文件。让我们用简单的Hello world显示开始PDF生成。

 <?php 
require('fpdf。 PHP');

$ pdf = new FPDF();
$ pdf-> AddPage();
$ pdf-> SetFont('Arial','B',16);
$ pdf->单元格(40,10,'Hello World!');
$ pdf-> Output();
?>

要生成pdf文件,首先我们需要包含库文件fpdf.php。那么我们需要使用默认的构造函数FPDF()创建一个FPDF对象。该构造函数可以传递三个值,即页面方向(纵向或横向),度量单位和页面大小(A4,A5等)。默认页面为A4纵向,测量单位为毫米。它可以用以下明确指定:

  $ pdf = new FPDF('P','mm','A4') ; 

可以使用景观(L),其他页面格式(如Letter和Legal)和测量单位(pt,cm,in)。



然后我们使用AddPage()向pdf文档添加了一个页面。原点在左上角,当前位置默认位于距离边界1厘米处;可以使用SetMargins()函数更改边距。



要打印文本,我们需要先使用SetFont()选择一个字体。让我们选择Arial bold 16:

  $ pdf-> SetFont('Arial','B',16); 

我们使用Cell()函数输出文本。单元格是一个矩形区域,可能是框架的,其中包含一些文本。它在当前位置输出。我们指定其尺寸,其文本(居中或对齐),如果绘制边框,以及当前位置在其后移动(向右,下方或下一行的开头)。要添加一个框架,我们将这样做:

  $ pdf->单元格(40,10,'Hello World! ,1); 

最后,文档关闭,并使用Output()发送到浏览器。我们可以通过传递所需的文件名将其保存在文件中。


I have a html document which marks up a report. I have a button on this page "Export as pdf". However I am not sure how to export html into a pdf..Are there any tools out there that anyone recommends for such a task..

EDIT: In more detail:

I have the following 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(){

   });
  </script>
  <title>Personal Diary System - Entry Report - <?php echo($date); ?></title>
  </head>
  <body>
  <h1>Entry Report - <?php echo($date); ?></h1>    
  <div id = "buttons">
  <a href = "http://dev.speechlink.co.uk">Export as PDF</a>                       
  </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{
       echo("User didn't leave any feedback");
     }
     echo("<br/>");
  ?>
  </body>
</html>
<?php
}
?>

This displays the following:

So if I'm using fpdf, what is the best way to export the following as a pdf? Should I write a fpdf function in the same php file or is it best to write a separate php file which creates and displays the pdf (which means I would have to post all relevant data to this file)...

解决方案

Use FPDF library for php check here

The first and the main base for this file conversion is FPDF library. FPDF is a pure PHP class to generate PDF files on the fly. Let us start the PDF generation with a simple Hello world display.

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

To generate a pdf file, first we need to include library file fpdf.php. Then we need to create an FPDF object using the default constructor FPDF(). This constructor can be passed three values namely page orientation (portrait or landscape), measure unit, and page size (A4, A5, etc.,). By default pages are in A4 portrait and the measure unit is millimeter. It could have been specified explicitly with:

$pdf=new FPDF('P','mm','A4');

It is possible to use landscape (L), other page formats (such as Letter and Legal) and measure units (pt, cm, in).

Then we have added a page to our pdf document with AddPage(). The origin is at the upper-left corner and the current position is by default placed at 1 cm from the borders; the margins can be changed with the function SetMargins().

To print a text, we need to first select a font with SetFont(). Let us select Arial bold 16:

$pdf->SetFont('Arial','B',16);

We use Cell() function to output a text. A cell is a rectangular area, possibly framed, which contains some text. It is output at the current position. We specify its dimensions, its text (centered or aligned), if borders should be drawn, and where the current position moves after it (to the right, below or to the beginning of the next line). To add a frame, we would do this:

$pdf->Cell(40,10,'Hello World !',1);

Finally, the document is closed and sent to the browser with Output(). We could have saved it in a file by passing the desired file name.

这篇关于用于将HTML导出为pdf的工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆