Index: pdfview/pdfview.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/pdfview/pdfview.module,v retrieving revision 1.18 diff -u -F^f -r1.18 pdfview.module --- pdfview/pdfview.module 28 Oct 2005 20:36:56 -0000 1.18 +++ pdfview/pdfview.module 26 May 2006 13:34:19 -0000 @@ -1,145 +1,190 @@ 'node/'. arg(1) .'/pdf', 'title' => t('view as pdf'), + 'callback' => 'pdfview_node_controller', + 'access' => user_access('access content as pdf'), + 'type' => MENU_CALLBACK + ); + $items[] = array('path' => 'pdfview/'. arg(1), 'title' => t('view as pdf'), + 'callback' => 'pdfview_node_controller', + 'access' => user_access('access content as pdf'), + 'type' => MENU_CALLBACK + ); + } + return $items; +} -function pdfview_help($section = 'admin/help#pdfview') { - $output = ''; - switch ($section) { - case 'admin/help#pdfview': - $output = t('View your nodes as pdf.'); - break; - case 'admin/modules#description': - $output = t('Enables users to view nodes as pdf.'); - break; - } - return $output; -} - -function pdfview_pdf ($node = 0, $depth = 1) { - if (user_access('access content as pdf')) { - //now using HTML2FPDF for more accuracy - //require_once('pdf.class.php'); - - require_once('html2fpdf/html2fpdf.php'); - - $pageheight = 770; - $pagewidth = 540; - - //now using HTML2FPDF for more accuracy - //$pdf = new PDF(); - $pdf = new HTML2FPDF(); - $pdf->Open(); - $pdf->AddPage(); - - echo $node->type; - - $node = node_load(array("nid" => $node)); - //$text = $node->body; - //$p = explode("\n", $text); - $output = $node->body; - $output = explode("\n", $output); - - $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") { - $image = image_display($node); - $pdf->WriteHTML($image); - } - - // Write the node's body to the PDF document. - $pdf->SetFont('helvetica', '', 11); - $param["height"] = 10; - foreach ($output as $one) { - $pdf->WriteHTML($one); - } - - $filename = strtr(strip_tags($node->title), " .,?!&#", "_______"); - header("Content-Disposition: attachment; filename=". $filename .".pdf"); - header("Content-Type: application/pdf"); - - print $pdf->Output($filename.'.pdf', 'D'); - die(); +/** + * Implementation of hook_link(). + */ +function pdfview_link($type, $node = 0, $main) { + $links = array(); + if ($type == 'node' && $main == 0) { + $links[] = theme('pdfview_link', $node); } + return $links; } +/** + * Implementation of hook_perm(). + */ function pdfview_perm() { - return array("access content as pdf"); + return array("access content as pdf"); } -function pdfview_page($nid) { - if (user_access('access content as pdf')) { - if ($node != null) - pdfview_pdf($nid); - else - switch (arg(1)) { - case 'view': - if (arg(2)) { - pdfview_pdf(arg(2)); - } - break; - default: - $output = t('You must specify which node you wish to view as pdf.'); - print theme('page', $output); - break; - } - } - else { - print theme('page', t("Access Denied"), message_access()); +/** + * Module functions. + */ +function pdfview_node_controller() { + $nid = arg(1); + if (is_numeric($nid)) { + pdfview_generate_file($nid); } } - -function pdfview_link($type, $node = null, $teaser = false) { - $links = array(); - if (($type == "node") && (user_access("access content as pdf"))) { - $links[] = l(t("view as pdf"), "pdfview/view/$node->nid", array("title" => t("View and print node as pdf."))); + +/** + * Outputs a pdf file. + */ +function theme_pdfview_generate_page($pdf, $nid) { + $node = node_load($nid); + $body = $node->body; + $body = explode("\n", $body); + $title = $node->title; + $author = t('By '). strip_tags(theme('username', $node)); + $published = t('Published').': '. format_date($node->created, 'small'); + + $pdf->AddPage(); + + //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 + $pdf->SetFont('helvetica','BI',10); + $pdf->MultiCell(0, 5, $author, 0, 'L', 0); + + //published + $pdf->SetFont('', '', 10); + $pdf->MultiCell(0, 5, $published, 0, 'L', 0); + $pdf->Ln(5); + + //node type specific stuff. + if ($node->type == "image") { + $image = image_display($node); + $pdf->WriteHTML($image); + } + //body + $pdf->SetFont('helvetica', '', 11); + $param["height"] = 10; + foreach ($body as $one) { + $pdf->WriteHTML($one); } - return $links; -} + return $pdf; +} -function pdfview_menu($may_cache) { - $items = array(); +/** + * Outputs an array of book nodes (children) + */ +function pdfview_book_recurse($pdf, $nid, $depth = 10) { + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.nid = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $nid); + while ($page = db_fetch_object($result)) { + $node = node_load($page->nid); + if ($node) { + $pdf = theme('pdfview_generate_page', $pdf, $node->nid); + $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $node->nid); + while ($childpage = db_fetch_object($children)) { + $childnode = node_load($childpage->nid); + if ($childnode->nid != $node->nid) { + $pdf = pdfview_book_recurse($pdf, $childnode->nid, $depth + 1); + } + } + } + } + return $pdf; +} + +/** + * Implementation of hook_form(). + */ +function pdfview_form_alter($form_id, &$form) { + //alter content type settings + if (isset($form['type']) && $form['type']['#value'].'_node_settings' == $form_id) { + $form['workflow']['pdfview_'. $form['type']['#value']] = array( + '#type' => 'radios', + '#title' => t('Download PDF'), + '#default_value' => variable_get('pdfview_'.$form['type']['#value'], 1), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t('Enable download footer link for those with access control permission.'), + ); + } +} + +/** + * Outputs a pdf file. + */ +function pdfview_generate_file($nid) { + global $base_url; - $items[] = array('path' => 'pdfview', 'title' => t('view as pdf'), - 'callback' => 'pdfview_page', - 'callback arguments' => $node->nid, - 'type' => MENU_CALLBACK, - 'access' => user_access('access content as pdf')); - return $items; + if (user_access('access content as pdf')) { + require_once(drupal_get_path('module', 'pdfview') . '/html2fpdf/html2fpdf.php'); + + $pageheight = 770; + $pagewidth = 540; + $pdf = new HTML2FPDF(); + $pdf->Open(); + + $initial_node = node_load($nid); + if (!isset($initial_node->parent)){ + $pdf = theme('pdfview_generate_page',$pdf, $nid); + } + else { //print book + $depth = count(book_location($initial_node)) + 1; + $pdf = pdfview_book_recurse($pdf, $nid, $depth); + } + + $filename = strtr(strip_tags($initial_node->title), " .,?!&#", "_______"); + drupal_set_header("Content-Disposition: attachment; filename=". $filename .".pdf"); + drupal_set_header("Content-Type: application/pdf"); + + print $pdf->Output($filename.'.pdf', 'D'); + die(); + } } - + +function theme_pdfview_link($node) { + if ((user_access('access content as pdf') || user_access('administer nodes')) && variable_get('pdfview_'.$node->type, 1) == 1) { + $attributes = array("title" => "Display a PDF version of this page."); + $links = l(t('download pdf'), "node/$node->nid/pdf", $attributes); + } + return $links; +} ?>