gpt4 book ai didi

php - ajax成功打开新窗口显示pdf

转载 作者:行者123 更新时间:2023-12-01 05:31:42 33 4
gpt4 key购买 nike

我使用 dompdf 创建一个 pdf 文档(即时)。我还必须将任何数据发布到此 pdf 中,因此我使用 ajax。

这是 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));
}
}

所以,我使用ajax发布大量数据来创建一个pdf文件到这样的php文件。

这是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');
}
});

这是使用 dompdf 的 php。

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');
}

这是要转为pdf的html

<!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>

我怎样才能让这个html pdf在ajax中成功打开到一个新的浏览器窗口中,有什么建议吗?感谢您的帮助,非常感谢。

最佳答案

如果您想避免将 pdf 文件保留在文件系统中,您的 ajax 处理程序 members/megumi/cek_list_wire_rod/generate_pdf_laporan 应该只在 session 中存储 $POSTed 数据让另一个 php 脚本(例如 get_pdf)使用 generate_pdf_laporan 函数使用此数据渲染 pdf。在这种情况下,您的 success 函数将非常简单:

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

建议在get_pdf中设置正确的MIME类型:

header('Content-Type: application/pdf');

关于php - ajax成功打开新窗口显示pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36834853/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com