开发者

Sending variables from jQuery to FPDF

I'm trying to send the contents of a form with jQuery to FPDF. The variables aren't showing but the pdf isn't giving an error either; it's just blank. I have only one variable so far for brevity. Any guesses?

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

EDIT

It looks like the problem lies in returning the generated pdf to the user. If I set the pdf to be saved on the server, the field values show up in the docume开发者_StackOverflownt.


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 an exit(); afterwards).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜