I'm using TCPDF class to generate PDF. First I send some data in json format (jquery) then I would like to generate PDF with these data.
我正在使用TCPDF类生成PDF。首先,我以json格式(jquery)发送一些数据,然后我想用这些数据生成PDF。
Could you tell me if I am right. I should generate pdf, save it on the server and then if ajax call returns success, redirect the user to this pdf. Is a good way?
你能告诉我我是不对的吗?我应该生成pdf,将其保存在服务器上,然后如果ajax调用返回成功,则将用户重定向到此pdf。好的方法?
I have:
我有:
$.ajax({
url: '/pdf/fiele_to_generate_pdf.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: $.toJSON(jsonObj),
processData: false,
dataType: 'html',
async: false,
success: function(html) {
window.location.href = '/pdf/example.pdf';
}
});
Thanks for any advice.
谢谢你的建议。
Edit: So I have sent a request to make the pdf via Ajax to the server, then the pdf file was generated and saved on the server. Then I have used window.location.href to redirect the user to this pdf. Everything works but I would like to avoid saving file on the server. Any ideas?
编辑:所以我发送了一个请求,通过Ajax将pdf发送到服务器,然后生成pdf文件并保存在服务器上。然后我使用window.location.href将用户重定向到此pdf。一切正常但我想避免在服务器上保存文件。有任何想法吗?
1 个解决方案
#1
9
Ok this script will save the file and also display in the browser
好的,这个脚本将保存文件并显示在浏览器中
the trick is in using the output method with the 'F' or 'I':
诀窍在于使用'F'或'I'的输出方法:
$pdf->Output("your path", 'F'); //save pdf
$pdf->Output('file.pdf', 'I'); // show pdf
CODE
<?php
class PDF extends TCPDF {
public function Header (){
$this->SetFont('dejavusans', '', 14);
$title = utf8_encode('title');
$subtitle = utf8_encode('sub title');
$this->SetHeaderMargin(40);
$this->Line(15,23,405,23);
}
public function Footer() {
$this->SetFont('dejavusans', '', 8);
$this-> Cell (0, 5, 'Pag '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
}
public static function makeHTML ($json){
$html = '<table border="0.5" cellspacing="0" cellpadding="4">
<tr>
<th bgcolor="#DAB926" style="width:3%; text-align:left"><strong>you th</strong></th>
</tr>';
for ($i=0; $i<count($json); $i++)
{
$a= $i+1;
$html .= '<tr>
<td style="width:15%; text-align:left">'.$json[$i]->Name.'</td>
</tr>';
}
$html .= '</table>';
return $html;
}
}
function printReport ($json )
{
set_time_limit(0);
$pdf = new PDF("L", PDF_UNIT, "A3",true, 'UTF-8', false);
$pdf->SetMargins (15, 27, 15, true);
$pdf->SetFont('dejavusans', '', 8);
$pdf->SetAutoPageBreak(TRUE,50);
$pdf->AddPage();
//create html
$html = $pdf->makeHTML($json);
$pdf->writeHTML($html, false, false, false, false, '');
if (!file_exists("your path"))
mkdir("your path");
$pdf->Output("your path", 'F'); //save pdf
$pdf->Output('file.pdf', 'I'); // show pdf
return true;
}
$json = json_decode("your json");
printReport($json);
?>
#1
9
Ok this script will save the file and also display in the browser
好的,这个脚本将保存文件并显示在浏览器中
the trick is in using the output method with the 'F' or 'I':
诀窍在于使用'F'或'I'的输出方法:
$pdf->Output("your path", 'F'); //save pdf
$pdf->Output('file.pdf', 'I'); // show pdf
CODE
<?php
class PDF extends TCPDF {
public function Header (){
$this->SetFont('dejavusans', '', 14);
$title = utf8_encode('title');
$subtitle = utf8_encode('sub title');
$this->SetHeaderMargin(40);
$this->Line(15,23,405,23);
}
public function Footer() {
$this->SetFont('dejavusans', '', 8);
$this-> Cell (0, 5, 'Pag '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
}
public static function makeHTML ($json){
$html = '<table border="0.5" cellspacing="0" cellpadding="4">
<tr>
<th bgcolor="#DAB926" style="width:3%; text-align:left"><strong>you th</strong></th>
</tr>';
for ($i=0; $i<count($json); $i++)
{
$a= $i+1;
$html .= '<tr>
<td style="width:15%; text-align:left">'.$json[$i]->Name.'</td>
</tr>';
}
$html .= '</table>';
return $html;
}
}
function printReport ($json )
{
set_time_limit(0);
$pdf = new PDF("L", PDF_UNIT, "A3",true, 'UTF-8', false);
$pdf->SetMargins (15, 27, 15, true);
$pdf->SetFont('dejavusans', '', 8);
$pdf->SetAutoPageBreak(TRUE,50);
$pdf->AddPage();
//create html
$html = $pdf->makeHTML($json);
$pdf->writeHTML($html, false, false, false, false, '');
if (!file_exists("your path"))
mkdir("your path");
$pdf->Output("your path", 'F'); //save pdf
$pdf->Output('file.pdf', 'I'); // show pdf
return true;
}
$json = json_decode("your json");
printReport($json);
?>