PDF-print

Gepost door Laurens R op 23-08-2010 01:22.

Dit is een klasse die Adobe JavaScript-code kan toevoegen aan een bestaand PDF-bestand.
De gebruiker krijgt, bij het openen van het bestand, dan meteen de vraag om het bestand af te drukken indien de lokale beveiligingsinstellingen dit toestaan.

Voorbeeld-gebruik:

<?php
$objBestand = new pdfFile("voorbeeld.pdf");
echo $objBestand;
?>

Bestanden van dit script

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
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)
    {
        $this->filename = $filename;
        $this->fcontents = file_get_contents($filename) or die("Incorrect filename");
        
        $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();        
    }
}
?>

Commentaar

Inloggen wachtwoord vergeten? Aanmelden
ADSL – Telfort Internet – Ziggo Alles in 1