【问题标题】:TCPDF not catching PHP variables via jQueryTCPDF 未通过 jQuery 捕获 PHP 变量
【发布时间】:2012-04-11 23:03:31
【问题描述】:

我通过 jQuery 向 TCPDF 发送用户数据,但它没有在 PDF 文件中显示从 jQuery 接收到的任何值。

我什至尝试捕获会话变量中的值,但它仍然不显示。我哪里错了?

HTML:

<div id="p">
    FOO
    <input type="text" id="nop" />
</div>

 <div id="e_table">
                <table>
                    <thead>
                        <th><h3>ABC</h3></th>
                        <th><h3>PQR</h3></th>
                        <th><h3>XYS</h3></th>
                    </thead>
                    <tbody>
                    <tr>
                        <td>Staff</td>
                        <td><input type="text" id="as" /></td>
                        <td><label id="las">label</label></td>
                    </tr>
                    <tr>
                        <td>Office</td>
                        <td><input type="text" id="bo"/></td>
                        <td><label id="lbo">label</label></td>
                    </tr>
                    <tr>
                        <td>Administrative</td>
                        <td><input type="text" id="mca" /></td>
                        <td><label id="lmca">label</label></td>
                    </tr>
                    </tbody>
                </table>
</div>

jQuery:

$('#pdf').click(function() {                    
                    var nop = $('#nop').val();
                    var as = $('#as').val();
                    var las = $('#las').text();
                    var bo = $('#bo').val();
                    var lbo = $('#lbo').text();
                    var mca = $('#mca').val();
                    var lmca = $('#lmca').text();

                    var postData = {
                         "nop" : nop,
                         "as" : as,
                         "las" : las,
                         "bo" : bo,
                         "lbo" : lbo,
                         "mca" : mca,
                         "lmca" : lmca
                    };

                    $.ajax({
                            type: "POST",
                            url: "pdf.php",
                            data: postData, 
                            success: function(data){
                                window.open("pdf.php"); 
                            }
                        });


        });

TCPPDF pdf.php:

<?php
session_start(); 

//============================================================+
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com s.r.l.
//               Via Della Pace, 11
//               09044 Quartucciu (CA)
//               ITALY
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * @author Nicola Asuni
 * @since 2009-03-20
 */

require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {


    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'B', 8);
        $this->SetTextColor(247, 147, 29); 


        $this->Cell(0, 10, 'ABC', 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ABC');
$pdf->SetTitle('ABC');
$pdf->SetSubject('ABC');
$pdf->SetKeywords('ABC');


// set default header data
$pdf->SetHeaderData(false);


$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 5, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------


// add a page
$pdf->AddPage();

$pdf->SetFont('helvetica', '', 8);

// -----------------------------------------------------------------------------


//request parameters
    $nop = strip_tags($_POST['nop']);
    $_SESSION['nop'] = $nop;
    $nop = $_SESSION['nop'];
    $as = strip_tags($_POST['as']);
    $las = strip_tags($_POST['las']);
    $bo = strip_tags($_POST['bo']);
    $lbo = strip_tags($_POST['lbo']);
    $mca = strip_tags($_POST['mca']);
    $lmca = strip_tags($_POST['lmca']);

$tbl = <<<EOF
          <div>
                <table border="0" cellpadding="5" cellspacing="2">
                    <tr>
                        <td>FOO</td>
                        <td>$nop</td>
                    </tr>
                    </tbody>
                </table>
              </div>
              <hr>
              <div>
                <table border="0" cellpadding="5" cellspacing="2">
                    <thead>
                    <tr>
                        <th><h3>ABC</h3></th>
                                        <th><h3>PQR</h3></th>
                                        <th><h3>XYS</h3></th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>Staff</td>
                        <td>$as</td>
                        <td>$las</td>
                    </tr>
                    <tr>
                        <td>Office</td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Administrative</td>
                        <td></td>
                        <td></td>
                    </tr>
                    </tbody>
                </table>
              </div>
         </div>
EOF;

$pdf->writeHTML($tbl, true, false, false, false, '');

$txt = $nop . 'This';

 // print a block of text using Write()
$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0); 
// -----------------------------------------------------------------------------


//Close and output PDF document
$pdf->Output('ABC.pdf', 'I');

//============================================================+
// END OF FILE                                                
//============================================================+

【问题讨论】:

    标签: php jquery variables pdf tcpdf


    【解决方案1】:

    您正在调用您的 pdf.php 2 次,一次带有发布数据,另一次没有

    $.ajax({ // call 1, with post data
        type: "POST",
        url: "pdf.php",
        data: postData, 
        success: function(data){
            window.open("pdf.php"); // call 2, without post data
        }
    });
    

    您可以做的是删除您的 ajax 请求,并通过 GET 发送数据,而不是使用

    var getString = "nop=" + nop "&as=" + as; // .....
    window.open("pdf.php?" + getString);
    

    【讨论】:

    • 感谢您的帮助。这行得通。我只想知道能不能把url字符串里的参数隐藏起来?
    • @input 不,抱歉,get 没有。取决于您要隐藏的原因,但很容易查看发送到的帖子值
    【解决方案2】:

    试试 print_r($_POST);

    并将其放在构建 pdf 的 php 文件开头的某个位置。 这有助于您了解哪些值到达,哪些值没有到达

    【讨论】:

      【解决方案3】:

      我什至尝试捕获会话变量中的值,但它仍然不显示。我哪里错了?

      它应该工作。你错过session_start()了吗?

      另一种方法是在您的 ajax 请求和输出文件名中生成 PDF,您可以使用 window.open() 打开它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 2011-09-24
        • 2021-03-21
        • 1970-01-01
        • 2016-08-20
        • 1970-01-01
        • 2017-03-11
        相关资源
        最近更新 更多