在PHP中将PHP变量传递给tcpdf时出现胎儿错误

在PHP中将PHP变量传递给tcpdf时出现胎儿错误

本文介绍了在PHP中将PHP变量传递给tcpdf时出现胎儿错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我传递php变量而不是pdf文件路径,如URL []但它显示下面描述的一些胎儿错误





胎儿错误:在C:未捕获的异常 'InvalidArgumentException' 有消息 '// 0无法打开VarStream!':\xampp\htdocs\VB\FPDI\pdf_parser.php:183堆栈跟踪: #0 C:\xampp\htdocs\VB\FPDI\fpdi_pdf_parser.php(64):pdf_parser-> __构建体( 'VarStream:// 0')#1 C:\xampp\htdocs\\ \\VB\FPDI\fpdi.php(135):fpdi_pdf_parser-> __构建体( 'VarStream:// 0')#2 C:\xampp\htdocs\VB\FPDI\fpdi.php( 106):FPDI-> _getPdfParser( 'VarStream:// 0')#3 C:\xampp\htdocs\VB\mlpdf.php(133):FPDI-> setSourceFile('VarStream:// 0')#4 C:\ xampp \ htdocs \VB \ TCPDF \ tcpdf.php(3537):PDF-> Heade R()#5 C:\xampp\htdocs\VB\TCPDF\tcpdf.php(3204):TCPDF->的setHeader()#6 C:\xampp\htdocs\VB\ TCPDF \tcpdf.php(3116):TCPDF-> startPage('','',false)#7 [内部函数]:TCPDF-> AddPage()#8 C:\ xampp \ htdocs \ VB\FPDI\fpdf_tpl.php(360):call_user_func_array(阵列,阵列)#9 C:\xampp\htdocs\VB\mlpdf.php(167):FPDF_TPL-> AddPage()#10 {main}抛出第183行的C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b

其中mlpdf.php是文件名



我尝试过:



here im passing php variable instead of pdf file path as said in URL Using a pdf from a php variable instead of a file | FAQ: Miscellaneous | Setasign[^] but it it shows some fetal error described below


fetal error:Uncaught exception 'InvalidArgumentException' with message 'Cannot open VarStream://0 !' in C:\xampp\htdocs\VB\FPDI\pdf_parser.php:183 Stack trace: #0 C:\xampp\htdocs\VB\FPDI\fpdi_pdf_parser.php(64): pdf_parser->__construct('VarStream://0') #1 C:\xampp\htdocs\VB\FPDI\fpdi.php(135): fpdi_pdf_parser->__construct('VarStream://0') #2 C:\xampp\htdocs\VB\FPDI\fpdi.php(106): FPDI->_getPdfParser('VarStream://0') #3 C:\xampp\htdocs\VB\mlpdf.php(133): FPDI->setSourceFile('VarStream://0') #4 C:\xampp\htdocs\VB\TCPDF\tcpdf.php(3537): PDF->Header() #5 C:\xampp\htdocs\VB\TCPDF\tcpdf.php(3204): TCPDF->setHeader() #6 C:\xampp\htdocs\VB\TCPDF\tcpdf.php(3116): TCPDF->startPage('', '', false) #7 [internal function]: TCPDF->AddPage() #8 C:\xampp\htdocs\VB\FPDI\fpdf_tpl.php(360): call_user_func_array(Array, Array) #9 C:\xampp\htdocs\VB\mlpdf.php(167): FPDF_TPL->AddPage() #10 {main} thrown in C:\xampp\htdocs\VB\FPDI\pdf_parser.php on line 183



where mlpdf.php is file name

What I have tried:

<?php
// just require TCPDF instead of FPDF
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');

$collcode=15485;


	$serverName = "KUDK126";


	$connectionInfo = array( "Database"=>"qp");
	$objConnect = sqlsrv_connect( $serverName, $connectionInfo);

$id=10;
	$strSQL = "SELECT * FROM files WHERE FilesID = $id ";
	$objQuery = sqlsrv_query($objConnect,$strSQL) or die ("Error Query [".$strSQL."]");

$objResult = sqlsrv_fetch_array($objQuery);
class VarStream extends FPDI{

	 private $_pos;
    private $_stream;
    private $_cDataIdx;

    static protected $_data = array();
    static protected $_dataIdx = 0;

    static function createReference(&$var) {
        $idx = self::$_dataIdx++;
        self::$_data[$idx] =& $var;
        return __CLASS__.'://'.$idx;
    }

    static function unsetReference($path) {
        $url = parse_url($path);
        $cDataIdx = $url["host"];
        if (!isset(self::$_data[$cDataIdx]))
            return false;

        unset(self::$_data[$cDataIdx]);

        return true;
    }

    public function stream_open($path, $mode, $options, &$opened_path) {
        $url = parse_url($path);
        $cDataIdx = $url["host"];
        if (!isset(self::$_data[$cDataIdx]))
            return false;
        $this->_stream = &self::$_data[$cDataIdx];
        $this->_pos = 0;
        if (!is_string($this->_stream)) return false;
        $this->_cDataIdx = $cDataIdx;
        return true;
    }

    public function stream_read($count) {
        $ret = substr($this->_stream, $this->_pos, $count);
        $this->_pos += strlen($ret);
        return $ret;
    }

    public function stream_write($data) {
        $l=strlen($data);
        $this->_stream =
            substr($this->_stream, 0, $this->_pos) .
            $data .
            substr($this->_stream, $this->_pos += $l);
        return $l;
    }

    public function stream_tell() {
        return $this->_pos;
    }

    public function stream_eof() {
        return $this->_pos >= strlen($this->_stream);
    }

    public function stream_seek($offset, $whence) {
        $l=strlen($this->_stream);
        switch ($whence) {
            case SEEK_SET: $newPos = $offset; break;
            case SEEK_CUR: $newPos = $this->_pos + $offset; break;
            case SEEK_END: $newPos = $l + $offset; break;
            default: return false;
        }
        $ret = ($newPos >= 0 && $newPos <= $l);
        if ($ret) $this->_pos=$newPos;
        return $ret;
    }

    public function url_stat($path, $flags) {
        $url = parse_url($path);
        $dataIdx = $url["host"];
        if (!isset(self::$_data[$dataIdx]))
            return false;

        $size = strlen(self::$_data[$dataIdx]);
        return array(
            7 => $size,
            'size' => $size
        );
    }

    public function stream_stat() {
        $size = strlen($this->_stream);

        return array(
            'size' => $size,
            7 => $size,
        );
    }


}

class PDF extends VarStream {

    var $_tplIdx;

    function Header() {

       // global $fullPathToFile;
stream_wrapper_register('VarStream', 'VarStream') or die('Failed to register protocol VarStream://');
        if (is_null($this->_tplIdx)) {

            // THIS IS WHERE YOU GET THE NUMBER OF PAGES

            $this->numPages = $this->setSourceFile(VarStream::createReference($objResult["FilesName"]));
            $this->_tplIdx = $this->importPage(1);

        }
        $this->useTemplate($this->_tplIdx);

    }

    function Footer() {}

}

// initiate PDF
//stream_wrapper_register('VarStream', 'VarStream') or die('Failed to register protocol VarStream://');
$pdf = new PDF();
$pdf->setFontSubsetting(true);

$style = array(
    'position' => '',
    'align' => 'C',
    'stretch' => false,
    'fitwidth' => true,
    'cellfitalign' => '',
    'border' => false,
    'hpadding' => 'auto',
    'vpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255),
    'text' => true,
    'font' => 'helvetica',
    'fontsize' => 8,
    'stretchtext' => 4
);

// add a page
$pdf->AddPage();
//$pdf->Cell(0, 0, 'CODE 128 AUTO', 0, 1);
$pdf->write1DBarcode('15748', 'C128', '', '', '', 15, 0.4, $style, 'N');

$pdf->Ln();

// The new content
$pdf->StartTransform();



		$pdf->Rotate(40, 50, 110);



		$pdf->SetFont("times", "B", 70);
		$pdf->SetAlpha(0.3);
		$pdf->Text(20,150,'Raghu');
		$pdf->StopTransform();
//$pdf->SetAlpha(2);

// THIS PUTS THE REMAINDER OF THE PAGES IN
if($pdf->numPages>1) {
    for($i=2;$i<=$pdf->numPages;$i++) {
        $pdf->endPage();
        $pdf->_tplIdx = $pdf->importPage($i);
        $pdf->AddPage();
		//$pdf->Cell(0, 0, 'CODE 128 AUTO', 0, 1);
$pdf->write1DBarcode('15748', 'C128', '', '', '', 15, 0.4, $style, 'N');

$pdf->Ln();
		$pdf->StartTransform();



		$pdf->Rotate(40, 50, 110);



		$pdf->SetFont("times", "B", 70);
		$pdf->SetAlpha(0.3);
		$pdf->Text(20,150,'Raghu');
		$pdf->StopTransform();
		//$pdf->Image('1.jpg', 90, 100, 60, 60, '', 'http://www.tcpdf.org', '', true, 72);
		//$pdf->SetAlpha(0.5);
    }
}

// Output the file as forced download
$pdf->Output();
?>

推荐答案




这篇关于在PHP中将PHP变量传递给tcpdf时出现胎儿错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:15