Login   Register  
PHP Classes
elePHPant
Icontem

File: class.pdffile-loadContents.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Laurens Ramandt  >  PDF print  >  class.pdffile-loadContents.php  >  Download  
File: class.pdffile-loadContents.php
Role: Auxiliary script
Content type: text/plain
Description: Modified version of the script to allow direct input of PDF contents, instead of loading from a file.
Class: PDF print
Alter a PDF document to make it print when opened
Author: By
Last change: Changed listing priority
Date: 3 years ago
Size: 2,104 bytes
 

Contents

Class file image Download
<?php
// Written by Laurens Ramandt
class pdfFile{
    private 
$filename;
    private 
$fcontents;
    private 
$lastObjectEnd;
    private 
$newObjectNr;
    private 
$insertedPrintCode=false;
    
    private function 
pdf_getHighestObjNr($pdfContents){
        
preg_match_all('[\d+\s0\s(obj)]'$pdfContents$matches);
        
        for(
$i=0$i<=count($matches[0])-1$i++){
            
$matches[0][$i] = str_replace(" 0 obj"""$matches[0][$i]);
        }
        return 
max($matches[0]);
    }    
    
    private function 
pdf_getLastObjEnd($pdfContents){
        return 
strripos($pdfContents"endobj" chr(13) . "xref" chr(13) . "0")+strlen("endobj" chr(13));    
    }
        
    public function 
__construct($filename$pdfContents="")
    {
        if(
strlen($filename) > 0){
            
$this->filename $filename;
            
$this->fcontents file_get_contents($filename) or die("Incorrect filename");
        }elseif(
strlen($pdfContents)>0){
            
$this->filename "";
            
$this->fcontents $pdfContents;
        }else{
            die(
"Couldn't load any PDF contents");
        }
        
        
$this->lastObjectEnd =    $this->pdf_getLastObjEnd($this->fcontents);
        
$this->newObjectNr =    $this->pdf_getHighestObjNr($this->fcontents)+1;
    }
        
    public function 
insertPrintCode(){
        if(!
$this->insertedPrintCode){
            
$insertion="";
            
$insertion .= ($this->newObjectNr) . " 0 obj" chr(13);
            
$insertion .= "<</S/JavaScript/JS(this.print\({bUI:true,bSilent:false,bShrinkToFit:true}\);)>>";
            
$insertion .= chr(13) . "endobj";
            
$this->fcontents substr_replace($this->fcontents$insertion chr(13),$this->lastObjectEnd,0);
            
$this->fcontents str_replace("/Type /Catalog ""/Type /Catalog " chr(13) . "/OpenAction " $this->newObjectNr " 0 R"$this->fcontents);
            
$this->insertedPrintCode=true;
        }        
    }
    
    public function 
saveAs($filename){
        @
unlink($filename);
        
$h=fopen($filename"w+");
        
fwrite($h$this->fcontents);
        
fclose($h);
    } 
    
    public function 
getFileContents(){
        return 
$this->fcontents;
    }
    
    public function 
__toString(){
        
header("Content-type: application/pdf");
        
$this->insertPrintCode();
        return 
$this->getFileContents();        
    }
}
?>