Index: pdfview.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/pdfview/pdfview.module,v retrieving revision 1.18 diff -u -r1.18 pdfview.module --- pdfview.module 28 Oct 2005 20:36:56 -0000 1.18 +++ pdfview.module 20 Jun 2006 21:46:23 -0000 @@ -1,145 +1,190 @@ 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); - } + case 'admin/help#pdfview': + return t('View your nodes as pdf.'); + case 'admin/modules#description': + return t('Enables users to view nodes as pdf.'); + } +} - $filename = strtr(strip_tags($node->title), " .,?!&#", "_______"); - header("Content-Disposition: attachment; filename=". $filename .".pdf"); - header("Content-Type: application/pdf"); +/** +* Implementation of hook_menu(). +*/ +function pdfview_menu($may_cache) { + $items = array(); + if (!$may_cache) { + $items[] = array('path' => '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; +} - 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"); } -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 pdfview_generate_page($pdf, $nid) { + if (user_access('access content as pdf')) { + $node = node_load($nid); + $body = check_markup($node->body); + $body = explode("\n", $body); + $title = $node->title; + $author = 'By '. strip_tags(theme('username', $node)); + $published = 'Published: '. format_date($node->created, 'small'); + + $pdf->setHeaderFont(Array('freesans', '', 10)); + $pdf->setFooterFont(Array('freesans', '', 8)); + + $pdf->AddPage(); + + //title + $pdf->SetFont('freesans','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('freesans','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('freesans', '', 11); + $param["height"] = 10; + foreach ($body as $one) { + $pdf->WriteHTML($one); + } + + return $pdf; + } +} + +/** +* 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 = 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.'), + ); } - - return $links; } -function pdfview_menu($may_cache) { - $items = array(); - - $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; +/** +* Outputs a pdf file. +*/ +function pdfview_generate_file($nid) { + if (user_access('access content as pdf')) { + require_once(drupal_get_path('module', 'pdfview') . '/tcpdf_php4/tcpdf.php'); + + $pdf = new TCPDF(); + $pdf->Open(); + + $node = node_load($nid); + if (!isset($node->parent)){ + $pdf = pdfview_generate_page($pdf, $nid); + } else { //print book + $depth = count(book_location($node)) + 1; + $pdf = pdfview_book_recurse($pdf, $nid, $depth); + } + + $filename = strtr(strip_tags($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; +}