【问题标题】:Convert Generated output html/css using php to pdf - Year 2017使用 php 将生成的输出 html/css 转换为 pdf - 2017 年
【发布时间】:2017-11-20 02:01:59
【问题描述】:

我产生如下的 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 文件?

非常感谢!

【问题讨论】:

    标签: php jquery css twitter-bootstrap pdf


    【解决方案1】:

    在你的 AJAX 中你可以调用 PHP 脚本,在这个 PHP 中你可以放这个代码:

    <?php
    $url = 'http://example.com/document.html';
    $data = json_decode(file_get_contents('http://api.rest7.com/v1/html_to_image.php?url=' . $url . '&format=pdf'));
    
    if (@$data->success !== 1)
    {
        die('Failed');
    }
    $pdf = file_get_contents($data->file);
    file_put_contents('rendered_page.pdf', $pdf);
    

    此示例使用指向 HTML 文件的 URL,但您可以使用 PHP 中的 CURL 扩展来执行到 http://api.rest7.com/v1/html_to_image.php 的 HTTP POST

    【讨论】:

    • 这些行做了什么 - $pdf = file_get_contents($data->file); file_put_contents('rendered_pa​​ge.pdf', $pdf);
    • $pdf = file_get_contents($data-&gt;file); 从 API 获取 PDF 文件。 file_put_contents('rendered_page.pdf', $pdf); 将获得的内容保存到文件 render_page.pdf
    【解决方案2】:

    您不能在 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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 2012-11-29
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      相关资源
      最近更新 更多