2

I use dompdf to create a pdf document (on the fly). I have to post any data too into this pdf, so I use ajax.

This is the PHP dompdf :

<?php

class Dompdfgenerator
{
 public function generate($html,$filename){
  define('DOMPDF_ENABLE_AUTOLOAD', false);
  require_once("./vendor/dompdf/dompdf/dompdf_config.inc.php");

  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $dompdf->stream($filename.'.pdf',array("Attachment"=>0));
 }
}

So,I use ajax to post a lot data to create a pdf file to a php file like this.

This is the ajax :

var result = $("#hasil-pencarian").clone().find('td:last-child').remove().end().html();

$.ajax({
 url: "<?= site_url('members/megumi/cek_list_wire_rod/generate_pdf_laporan') ?>",
 type: 'POST',
 data: {
    result: result,
    id_pendukung: $('.printLaporan').attr('data-id')
 },
 dataType: 'json',
 success: function (response) {

   open a new window


 },
 error: function () {
    alert('Terjadi masalah di server saat print laporan');
 }
});

And this is the php to use dompdf.

public function generate_pdf_laporan() {
    $this->load->library('dompdfgenerator');

    $data= array(
       'result' => $this->input->post('result')
    );

    $html = $this->load->view('members/megumi/check_list_of_wire_rod/v_laporan_check_list', $data, true);

    $this->dompdfgenerator->generate($html, 'contoh');
}

This is the html to be a pdf

<!DOCTYPE html>
<html>
<head>
<title>Report Table</title>
<style type="text/css">
#outtable{
  padding: 20px;
  border:1px solid #e3e3e3;
  width:600px;
  border-radius: 5px;
}

.short{
  width: 50px;
}

.normal{
  width: 150px;
}

table{
  border-collapse: collapse;
  font-family: arial;
  color:#5E5B5C;
}

thead th{
  text-align: left;
  padding: 10px;
}

tbody td{
  border-top: 1px solid #e3e3e3;
  padding: 10px;
}

tbody tr:nth-child(even){
  background: #F6F5FA;
}

tbody tr:hover{
  background: #EAE9F5
}
</style>
</head>
<body>
<div id="outtable">
  <table>
    <thead>
        <tr>
            <th class="short">No</th>
            <th class="normal">First Name</th>
            <th class="normal">Last Name</th>
            <th class="normal">Username</th>
        </tr>
    </thead>
    <tbody>
        <?php echo $result ?>
    </tbody>
  </table>
 </div>

How can I make this html pdf will be openede into a new browser window in ajax success, any suggestion ? Thanks for the help, it so appreciated.

Fadly Dzil
  • 2,154
  • 3
  • 34
  • 85
  • If you were to use AJAX to tell it was successful, you need to save the PDF locally on the server and provide a reference to the saved location on the returning AJAX; in which you direct the user too. – MackieeE Apr 25 '16 at 08:00
  • Not an exactly duplicate, but this [link](http://stackoverflow.com/questions/9399354/how-to-open-a-new-window-and-insert-html-into-it-using-jquery) solves what you are asking for. – Dacklf Apr 25 '16 at 11:12

2 Answers2

0

Use window.open

window.open("http://path_pdf_file");

php for saving the pdf to disk:

$output = $dompdf->output();
    file_put_contents($filename.'.pdf', $output);
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

If you like to avoid to persist pdf file in the file system, your ajax handler members/megumi/cek_list_wire_rod/generate_pdf_laporan should just store $POSTed data in session to let another php script, let's say get_pdf, to use generate_pdf_laporan function to render the pdf using this data. In this case your success function would be as simple as:

success: function (response) {
    window.open('/members/megumi/cek_list_wire_rod/get_pdf', '_blank');
}

It is recommended to set correct MIME type in get_pdf:

header('Content-Type: application/pdf');
Alex Blex
  • 34,704
  • 7
  • 48
  • 75