Index: CHANGELOG =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/pdfview/CHANGELOG,v retrieving revision 1.3 diff -u -r1.3 CHANGELOG --- CHANGELOG 3 Apr 2004 19:16:37 -0000 1.3 +++ CHANGELOG 23 May 2004 00:22:53 -0000 @@ -1,7 +1,14 @@ -12. March 2004 -============== -Updated to work with Drupal 4.4. - +22. May 2004 +============ +Now working with FPDF. +Improved image support. +Support for various HTML tags: + BR, P, BLOCKQUOTE, STRONG, EM, B, I, U, A, IMG, FONT, HR + +12. March 2004 +============== +Updated to work with Drupal 4.4. + 6. May 2003 =========== strip tags from text. Index: INSTALL =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/pdfview/INSTALL,v retrieving revision 1.2 diff -u -r1.2 INSTALL --- INSTALL 3 Apr 2004 19:16:37 -0000 1.2 +++ INSTALL 23 May 2004 00:22:53 -0000 @@ -1,5 +1,5 @@ -1) Copy pdfview.module to module/pdfview directory of your site. -2) Retrieve phppdflib from the location given in README. -3) Copy all of the php files to modules/pdfview/phppdflib. -4) Enable the module in site configuration modules. -5) Set permissions for access. +1) Copy pdfview.module to module/pdfview directory of your site. +2) Retrieve FPDF from the location given in README. +3) Copy the entire directory structure from the archive to modules/pdfview/fpdf. +4) Enable the module in site configuration modules. +5) Set permissions for access. Index: README =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/pdfview/README,v retrieving revision 1.3 diff -u -r1.3 README --- README 7 Jan 2003 14:41:31 -0000 1.3 +++ README 23 May 2004 00:22:53 -0000 @@ -1,9 +1,10 @@ A module to generate a pdf files from nodes. -It relies on phppdflib which is released by Potential Technologies. -It can be found here: http://www.potentialtech.com/ppl.php -Since it is under heavy development I do not include the class here. +It relies on FPDF, which is a freeware PHP class. +It can be found here: http://www.fpdf.org -This module requires the 1.x-branch of phppdflib. +The FPDF distribution comes with instructions for handling custom fonts and +encodings. Please refer to their documentation before posting a feature or +support requestion on http://drupal.org Send bug reports to gerhard@killesreiter.de. Index: pdfview.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/pdfview/pdfview.module,v retrieving revision 1.14 diff -u -r1.14 pdfview.module --- pdfview.module 13 Mar 2004 14:52:59 -0000 1.14 +++ pdfview.module 23 May 2004 00:22:53 -0000 @@ -4,19 +4,19 @@ /** * Last tested: * By: Gabriel Sjoberg (aim:banditbuddhist email:xabbu-at-cox-dot-net drupal: TheLibrarian) - * Running At: http://thelibrarian.dyndns.org:8080 - * Date: 12 March 2004 - * Drupal Version: cvs (12 March 2004) + * Running At: http://www.nosir.org + * Date: 22 May 2004 + * Drupal Version: cvs (22 May 2004) */ /** * @todo Expand the help text. * @todo Comment the source code for this module. * @todo Improve support for images. - * @todo Center title horizontally on the page. * @todo Add support for printing entire books to pdf (this may have to be implemented in book.module). */ + function pdfview_help($section = "admin/help#pdfview") { $output = ""; switch ($section) { @@ -32,77 +32,65 @@ function pdfview_pdf($node) { if(user_access("access content as pdf")) { - include_once "modules/pdfview/phppdflib.class.php"; - $pdf = new pdffile; - $pdf->set_default('margin', 0); - - $param["height"] = 11; - $param["fillcolor"] = $pdf->get_color('black'); - $param["font"] = "Helvetica"; - $param["rotation"] = 0; + require_once('pdf.class.php'); $pageheight = 770; $pagewidth = 540; - $paper = "a4"; + $node = node_load(array("nid" => $node)); - $text = strip_tags($node->body); + $text = $node->body; $p = explode("\n", $text); - $top = $pageheight; - $page = $firstpage = $pdf->new_page($paper); - // Every node has a title. Leave two empty rows afterwards. - $title = array($node->title, " ", " "); - $param["height"] = 14; - foreach ($title as $one) { - while (is_string($one)) { - $one = $pdf->draw_one_paragraph($top, 72, 72, $pagewidth, $one, $page, $param); - if (is_string($one)) { - $page = $pdf->new_page($paper); - $top = $pagewidth; - } - else { - $top = $one; - } - } - } + $pdf = new PDF(); + $pdf->Open(); + $pdf->AddPage(); + + $title = $node->title; + $pdf->SetFont('helvetica','B',15); + $w = $pdf->GetStringWidth($title)+6; + $pdf->SetX((210-$w)/2); + $pdf->SetLineWidth(1); + $pdf->Cell($w,9,$title,0,1,'C'); + $pdf->Ln(5); + + $author = 'By '. strip_tags(format_name($node)); + $pdf->SetFont('helvetica','BI',10); + $pdf->MultiCell(0, 5, $author, 0, 'L', 0); + + $published = 'Published: '. format_date($node->created, 'small'); + $pdf->SetFont('', '', 10); + $pdf->MultiCell(0, 5, $published, 0, 'L', 0); + $pdf->Ln(5); + // Node type specific stuff. if ($node->type == "image") { - $filename = $node->image_path; - $extension = $node->format; - $fh = fopen($filename, "r"); - $filedata = fread($fh, filesize($filename)); - fclose($fh); - $top = $top - $node->height - 20 ; - $left = ($pagewidth - $node->width)/2; - if ($extension == "jpg" || $extension == "jpeg") { - $image = $pdf->jfif_embed($filedata); - $placement = $pdf->image_place($image, $top + 10 , $left, $page); - } - else if ($extension == "png") { - $image = $pdf->png_embed($filedata); - $placement = $pdf->image_place($image, $top + 10, $left, $page); - } + $filename = $node->image_path; + $extension = $node->format; + $top = $top - $node->height - 20 ; + $left = ($pagewidth - $node->width)/2; + $width = $node->width; + $height = $node->height; + if ($width > $pagewidth) { + $proportion = $pagewidth / $width; + $width = $pagewidth; + $height = $height * $proportion; + } + $pdf->WriteHTML(''); } + // Write the node's body to the PDF document. + $pdf->SetFont('helvetica', '', 11); $param["height"] = 10; foreach ($p as $one) { - while (is_string($one)) { - $one = $pdf->draw_one_paragraph($top, 72, 72, $pagewidth, $one, $page, $param); - if (is_string($one)) { - $page = $pdf->new_page($paper); - $top = $pageheight; - } - else { - $top = $one; - } - } + $pdf->WriteHTML($one); } + $filename = strtr(strip_tags($node->title), " .,?!&#", "_______"); header("Content-Disposition: attachment; filename=". $filename .".pdf"); header("Content-Type: application/pdf"); - print $pdf->generate(9); + print $pdf->Output($filename.'.pdf', 'D'); die(); } } Index: contributions/modules/pdfview/pdf.class.php =================================================================== RCS file: contributions/modules/pdfview/pdf.class.php diff -N contributions/modules/pdfview/pdf.class.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ contributions/modules/pdfview/pdf.class.php 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,253 @@ +FPDF($orientation,$unit,$format); + //Initialization + $this->B=0; + $this->I=0; + $this->U=0; + $this->HREF=''; + $this->fontlist=array("arial","times","courier","helvetica","symbol"); + $this->issetfont = false; + $this->issetcolor = false; + $this->isblockquote = false; +} + +function Footer() +{ + //Page footer + $this->SetY(-15); + $this->SetFont('Arial','I',8); + $this->SetTextColor(128); + $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C'); +} + +////////////////////////////////////// +//html parser + +function WriteHTML($html) +{ + //remove all unsupported tags + $html=strip_tags($html,"


"); + $html=str_replace("\n",' ',$html); //replace carriage returns by spaces + $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //explodes the string + foreach($a as $i=>$e) + { + if($i%2==0) + { + // Print the text. + if($this->HREF) { + $this->PutLink($this->HREF,$e); + } + else if($this->ALIGN == 'center') { + $this->Cell(0,5,$e,0,1,'C'); + } + else if ($this->isblockquote == true) { + $this->MultiCell(0, 5, $e, 0, 'J', 1); + } + else + $this->Write(5,stripslashes(txtentities($e))); + } + else + { + //Tag + if($e{0}=='/') + $this->CloseTag(strtoupper(substr($e,1))); + else + { + //Extract attributes + $a2=explode(' ',$e); + $tag=strtoupper(array_shift($a2)); + $attr=array(); + foreach($a2 as $v) { + if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3)) { + $attr[strtoupper($a3[1])]=$a3[2]; + } + } + $this->OpenTag($tag,$attr); + } + } + } +} + +function OpenTag($tag,$attr) +{ + //Opening tag + switch($tag){ + case 'STRONG': + $this->SetStyle('B',true); + break; + case 'EM': + $this->SetStyle('I',true); + break; + case 'B': + case 'I': + case 'U': + $this->SetStyle($tag,true); + break; + case 'A': + $this->HREF=$attr['HREF']; + break; + case 'IMG': + if(isset($attr['SRC']) and (isset($attr['WIDTH']) or isset($attr['HEIGHT']))) { + if(!isset($attr['WIDTH'])) { + $attr['WIDTH'] = 0; + } + if(!isset($attr['HEIGHT'])) { + $attr['HEIGHT'] = 0; + } + $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT'])); + } + break; + case 'TR': + case 'BR': + $this->Ln(5); + break; + case 'P': + $this->ALIGN=$attr['ALIGN']; + break; + case 'FONT': + if (isset($attr['COLOR']) and $attr['COLOR']!='') { + $coul=hex2dec($attr['COLOR']); + $this->SetTextColor($coul['R'],$coul['G'],$coul['B']); + $this->issetcolor=true; + } + if (isset($attr['FACE']) and in_array(strtolower($attr['FACE']), $this->fontlist)) { + $this->SetFont(strtolower($attr['FACE'])); + $this->issetfont=true; + } + break; + case 'BLOCKQUOTE': + $this->Ln(10); + $this->SetFillColor(238); + $this->SetLeftMargin(25); + $this->SetRightMargin(25); + $this->isblockquote = true; + break; + case 'HR': + if( $prop['WIDTH'] != '' ) { + $Width = $prop['WIDTH']; + } + else { + $Width = $this->w - $this->lMargin - $this->rMargin; + } + $this->Ln(2); + $x = $this->GetX(); + $y = $this->GetY(); + $this->SetLineWidth(0.4); + $this->Line($x,$y,$x+$Width,$y); + $this->SetLineWidth(0.2); + $this->Ln(2); + break; + } +} + +function CloseTag($tag) +{ + //Closing tag + switch($tag) { + case 'STRONG': + $tag='B'; + case 'EM': + $tag='I'; + case 'B': + case 'I': + case 'U': + $this->SetStyle($tag,false); + break; + case 'A': + $this->HREF=''; + break; + case 'FONT': + if ($this->issetcolor) { + $this->SetTextColor(0); + } + if ($this->issetfont) { + $this->SetFont('arial'); + $this->issetfont=false; + } + break; + case 'P': + $this->ALIGN=''; + break; + case 'BLOCKQUOTE': + $this->SetFillColor(255); + $this->SetMargins(1, 1, 1); + $this->isblockquote = false; + $this->Ln(10); + break; + } +} + +function SetStyle($tag,$enable) +{ + //Modify style and select corresponding font + $this->$tag+=($enable ? 1 : -1); + $style=''; + foreach(array('B','I','U') as $s) { + if($this->$s>0) { + $style.=$s; + } + } + $this->SetFont('',$style); +} + +function PutLink($URL,$txt) +{ + //Put a hyperlink + $this->SetTextColor(0,0,255); + $this->SetStyle('U',true); + $this->Write(5,$txt,$URL); + $this->SetStyle('U',false); + $this->SetTextColor(0); +} + + +} + +//function hex2dec +//returns an associative array (keys: R,G,B) from +//a hex html code (e.g. #3FE5AA) +function hex2dec($couleur = "#000000"){ + $R = substr($couleur, 1, 2); + $rouge = hexdec($R); + $V = substr($couleur, 3, 2); + $vert = hexdec($V); + $B = substr($couleur, 5, 2); + $bleu = hexdec($B); + $tbl_couleur = array(); + $tbl_couleur['R']=$rouge; + $tbl_couleur['G']=$vert; + $tbl_couleur['B']=$bleu; + return $tbl_couleur; +} + +//conversion pixel -> millimeter in 72 dpi +function px2mm($px){ + return $px*25.4/72; +} + +function txtentities($html){ + $trans = get_html_translation_table(HTML_ENTITIES); + $trans = array_flip($trans); + return strtr($html, $trans); +} + +?>