Click here to Skip to main content
15,891,661 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
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();
?>
Posted
Updated 23-Jun-17 7:32am

1 solution

stream_wrapper_register('VarStream', 'VarStream') or die('Failed to register protocol VarStream://'); this should be placed before class PDF extends begins
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900