php - 使用 php 将生成的输出 html/css 转换为 pdf - 2017 年

标签 php jquery css twitter-bootstrap pdf

我生成如下所示的 php 输出

$.fn.getPDF = function(folder_suffix){
  var fullurl = folder_suffix+"/superimpressivereport.php";
  $.ajax({
     url: fullurl,
     dataType:'html'
      }).done(function(data) { 
         $.fn.converthtmltopdf(data):
      });   
 };
$.fn. fn.converthtmltopdf = function(htmlcssoutput){
   //TODO
}

我想将使用 bootstrap 和 css 生成的输出 html 转换为整洁的 pdf 文件。有没有我可以用来动态生成 pdf 的库,以便用户可以在他们的本地计算机上生成和下载 pdf 文件?

非常感谢!

最佳答案

您不能在 Javascript 中执行此操作 对于 pdf ,您可以使用 http://www.fpdf.org/

它非常易于使用,下面是为事件创建带有条形码的门票的示例:

$pdf = new FPDF();

$pdf->AddPage("H",array(100,160));
$pdf->AddFont('code128',"", 'code128.php');
$pdf->AddFont('3of9',"", '3of9.php');

$pdf->Image('img/'.$alias.'/ticket.gif', -20, 0, 180, 100);

//blank
$pdf->SetFont('arial','',6);
$pdf->Cell(145,10,'', 0,1,'R');

// EVENT DATE
$pdf->SetFont('arial','',6);
$pdf->Cell(145,3,'Event Date:', 0,1,'R');

$pdf->SetFont('arial','B',8);
$pdf->Cell(145,5,$event_date, 0,1,'R');


// EVENT TIME
$pdf->SetFont('arial','',6);
$pdf->Cell(145,3,'Event Time:', 0,1,'R');

$pdf->SetFont('arial','B',8);
$pdf->Cell(145,5,$event_time, 0,1,'R');


// TICKET SERIAL
$pdf->SetFont('arial','',6);
$pdf->Cell(145,5, "Class: $class_code | Serial Number: $serial_number", 0,1,'R');
$pdf->SetFont('3of9','',10);
$pdf->Cell(145,5, "*$serial_number*", 0,1,'R');


// TICKET PASS
$pdf->SetFont('arial','',6);
$pdf->Cell(145,5, "Passcode: $passcode", 0,1,'R');
$pdf->SetFont('3of9','',10);
$pdf->Cell(145,5, "*$passcode*", 0,1,'R');


// PURCHASE BY
$pdf->SetFont('arial','',6);
$pdf->Cell(145,3, "Purchased by : $purchased_by", 0,1,'R');

// Payment Method
$pdf->SetFont('arial','',6);
$pdf->Cell(145,3, "Payment Method : $payment_method", 0,1,'R');

// Transaction ID
$pdf->SetFont('arial','',6);
$pdf->Cell(145,3, "Transaction ID : $transaction_id", 0,1,'R');

if ($method == "display") {
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="the.pdf"');
    header('Content-Transfer-Encoding: binary');
    return $pdf->Output();
} else {
    return $pdf->Output("$filename", "S");
}

关于php - 使用 php 将生成的输出 html/css 转换为 pdf - 2017 年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603708/

相关文章:

php - jquery.countdown 日期时间问题

javascript - 如何动态地将 lodash 对象(过滤器对象)转换为 jquery 的 ListView

javascript - 用于扩展 jQuery 的奇怪语法

javascript - jQuery-ui 的调整大小功能无法与 chrome 中的 flexbox 模型正确交互,但在 FF 和 IE 中成功

css - 响应式移动网站设计

php - 将透明背景颜色应用到特定表格[行][列]

php - mcrypt 加密将 s 串 '%00' 添加到字符串末尾

PHP - 获取浮点变量内部值

javascript - div之间的等边距

javascript - 如果设备在移动设备上,则更改 css