将变量从 jQuery 发送到 FPDF
我正在尝试使用 jQuery 将表单的内容发送到 FPDF。变量没有显示,但 pdf 也没有给出错误;它只是空白。为简洁起见,到目前为止我只有一个变量。有什么猜测吗?
Form
<form id="contact">
<input type="hidden" name="front_finish_name_field" id="front_finish_name_field" value="Mirror" />
<input type="hidden" name="front_finish_price_field" id="front_finish_price_field" value="15" />
<input type="hidden" name="front_pattern_name_field" id="front_pattern_name_field" value="Tiger" />
<input type="hidden" name="front_pattern_price_field" id="front_pattern_price_field" value="5" /><br />
<input type="hidden" name="back_finish_name_field" id="back_finish_name_field" value="Mirror" />
<input type="hidden" name="back_finish_price_field" id="back_finish_price_field" value="15" />
<input type="hidden" name="back_pattern_name_field" id="back_pattern_name_field" value="Tiger" />
<input type="hidden" name="back_pattern_price_field" id="back_pattern_price_field" value="5" /><br />
<input type="hidden" name="glass_total" id="glass_total" value="40" />
<br />
<h2>Send Quote</h2><br />
Select agent: <select name="agents" id="agents">
<option value="1">Jill Smith</option>
<option value="2">John Smith</option>
<option value="3">Jack Smith</option>
</select>
<input type="submit" id="submit" value="Send" />
</form>
jQuery
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#submit").click(function(){
var data = $("#contact").serialize();
$.ajax
({
type: "POST",
url: "generate_pdf.php",
data: data,
cache: false,
success: function()
{
alert("Thank you");
}
});
return false;
});
});
</script>
FPDF php
<?php
require('fpdf.php');
$front_finish_name_field = $_POST['front_finish_name_field'];
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$front_finish_name_field);
$pdf->Output( "sample.pdf", "I" );
?>
编辑
看起来问题在于将生成的pdf返回给用户。如果我将 pdf 设置为保存在服务器上,则字段值将显示在文档中。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
生成的 PDF 输出是有效的 PDF 吗?如果您不小心在 PDF 序言之前写入了空格,该文件可能会显示为空白(因为读者不会首先看到 PDF 魔术字符)。
我会非常非常仔细地检查 PHP 文件,并确保文件中的第一个字符是 或
,绝对没有空格(新在此之前,输出 PDF 数据后绝对没有空格(尝试在其后放置
exit();
)。Is the PDF output being generated a valid PDF? If you are accidentally writing whitespace before the PDF preamble the file may show up as blank (because the reader doesn't see the PDF magic characters as the very first thing).
I would very very carefully check the PHP file and make sure the very first characters in the file are
<php
or<?
, that there is absolutely NO whitespace (new lines, tabs, spaces) before that and that there is absolutely no whitespace after you output the PDF data (try putting anexit();
afterwards).像这样将 PDF 内容粘贴到 SO 中通常不是一个好主意,但它确实向我展示了一些重要的东西:
该文件中唯一的字体是“Helvetica”,而不是“Arial”......哎呀。根据 FPDF 文档,Arial 只是 Helvetica 的同义词。它是“Helvetica-Bold”。好的。所以你的 SetFont 调用并没有被简单地忽略。这是个好消息。
页面内容流的压缩长度足够长,让我觉得里面有东西,可能是您的文本(以您看不到的方式绘制)。
您能给我们一个 PDF 链接吗?看似有效的文本不可见的原因有很多。
It's generally not a very good idea to paste PDF contents into SO like that, but it did show me something important:
The only font in that file is "Helvetica", not "Arial"... Whoops. According to the FPDF docs, Arial is just a synonym for Helvetica as far as they're concerned. And it is "Helvetica-Bold". Okay. So your SetFont call wasn't simply ignored. That's good news.
And the compressed length of the page's content stream is long enough to make me think SOMETHING is in there, possibly your text (drawn in a way you cannot see).
Can you give us a link to the PDF? There are any number of reasons why apparently-valid text isn't visible.