简体   繁体   中英

Convert Generated output html/css using php to pdf - Year 2017

I produce php output like the following

$.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
}

I want to convert the generated output html that uses bootstrap and css to a neat pdf file. Is there a library that I can use to generate pdf on the fly so that users can generate and download the pdf file in their local computer?

Thanks a ton!

You can't do this in Javascript for pdf , you can use http://www.fpdf.org/

it's very easy to use, below is the sample to create a ticket with barcode for an event:

$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");
}

In your AJAX you can call PHP script and inside this PHP you can put this code:

<?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);

This example uses URL to the HTML file but you can use CURL extension in PHP to perform a HTTP POST to http://api.rest7.com/v1/html_to_image.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM