/[drupal]/contributions/modules/pdfview/pdfview.module
ViewVC logotype

Contents of /contributions/modules/pdfview/pdfview.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.44 - (show annotations) (download) (as text)
Mon Feb 5 15:29:05 2007 UTC (2 years, 9 months ago) by egonbianchet
Branch: MAIN
CVS Tags: HEAD
Changes since 1.43: +3 -2 lines
File MIME type: text/x-php
#116065: don't die(), use module_invoke_all('exit') and exit() instead. Patch by Tobias Maier.
1 <?php
2 // $Id: pdfview.module,v 1.43 2007/02/05 15:19:50 egonbianchet Exp $
3
4 /**
5 * @file
6 * Renders nodes as PDF files for download
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function pdfview_help($section) {
13 switch ($section) {
14 case 'admin/help#pdfview':
15 return t('View your nodes as PDF.');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function pdfview_menu($may_cache) {
23 $items = array();
24 if (!$may_cache) {
25 $items[] = array('path' => 'node/'. arg(1) .'/pdf', 'title' => t('view as PDF'),
26 'callback' => 'pdfview_node_controller',
27 'access' => user_access('access content as PDF'),
28 'type' => MENU_CALLBACK
29 );
30 $items[] = array('path' => 'pdfview/'. arg(1), 'title' => t('view as PDF'),
31 'callback' => 'pdfview_node_controller',
32 'access' => user_access('access content as PDF'),
33 'type' => MENU_CALLBACK
34 );
35 }
36 return $items;
37 }
38
39 /**
40 * Implementation of hook_link().
41 */
42 function pdfview_link($type, $node = 0, $main) {
43 $links = array();
44 if ($type == 'node' && $main == 0) {
45 if (user_access('access content as PDF') && variable_get('pdfview_'.$node->type, 1)) {
46 $links['pdfview_link'] = array(
47 'title' => t('Download PDF'),
48 'href' => "node/$node->nid/pdf",
49 'attributes'=> array('title' => t('Display a PDF version of this page.'))
50 );
51 }
52 }
53 return $links;
54 }
55
56 /**
57 * Implementation of hook_perm().
58 */
59 function pdfview_perm() {
60 return array("access content as PDF");
61 }
62
63 /**
64 * Implementation of hook_form_alter().
65 */
66 function pdfview_form_alter($form_id, &$form) {
67 //alter content type settings
68
69 if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
70 $form['workflow']['pdfview'] = array(
71 '#type' => 'radios',
72 '#title' => t('PDF download'),
73 '#default_value' => variable_get('pdfview_'.$form['identity']['type']['#default_value'], 1),
74 '#options' => array(t('Disabled'), t('Enabled')),
75 );
76 }
77 }
78
79 /**
80 * Menu callback
81 */
82 function pdfview_node_controller() {
83 $nid = arg(1);
84 if (is_numeric($nid)) {
85 $node = node_load($nid);
86 if (variable_get('pdfview_'.$node->type, 1)) {
87 $pdf = pdfview_node($node);
88 pdfview_generate_file($pdf, $node->title);
89 }
90 }
91 }
92
93 /**
94 * Renders a node as a PDF file.
95 */
96 function pdfview_node($node) {
97 $path = drupal_get_path('module', 'pdfview');
98 require_once($path . '/tcpdf/tcpdf.php');
99
100 $pdf = theme('pdfview_pdf');
101
102 if ($node->type == 'book') {
103 $depth = count(book_location($node)) + 1;
104 $pdf = pdfview_book_recurse($pdf, $node, $depth);
105 }
106 else {
107 $pdf = theme('pdfview_node', $pdf, $node);
108 }
109
110 return $pdf;
111 }
112
113 /**
114 * Renders a book as a PDF file.
115 */
116 function pdfview_book_recurse($pdf, &$node, $depth = 10) {
117 if ($node) {
118 $pdf = theme('pdfview_node', $pdf, $node);
119
120 $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);
121
122 while ($childpage = db_fetch_object($children)) {
123 $childnode = node_load($childpage->nid);
124 if ($childnode->nid != $node->nid) {
125 $pdf = pdfview_book_recurse($pdf, $childnode, $depth + 1);
126 }
127 }
128 }
129 return $pdf;
130 }
131
132 /**
133 * Outputs a PDF file.
134 */
135 function pdfview_generate_file(&$pdf, $filename) {
136 $filename = strtr(strip_tags($filename), " .,?!&#", "_______");
137 print $pdf->Output($filename.'.pdf', 'I');
138 module_invoke_all('exit');
139 exit;
140 }
141
142 /**
143 * Open a new PDF document and set its properties
144 * @ingroup themeable
145 */
146 function theme_pdfview_pdf() {
147 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);
148 $pdf->Open();
149
150 // set margins
151 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
152 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
153 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
154
155 // set auto page breaks
156 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
157 $pdf->AliasNbPages();
158
159 // set image scale factor
160 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
161
162 $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
163 $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
164
165 return $pdf;
166 }
167
168 /**
169 * Renders PDF output for a node
170 * @ingroup themeable
171 */
172 function theme_pdfview_node(&$pdf, $node) {
173 $pdf->AddPage();
174
175 $pdf = theme('pdfview_title', $pdf, $node->title);
176
177 $author = strip_tags(theme('username', $node));
178 $pdf = theme('pdfview_author', $pdf, $author);
179
180 $pdf = theme('pdfview_published', $pdf, $node->created);
181
182 if (node_hook($node, 'view')) {
183 node_invoke($node, 'view');
184 }
185 else {
186 $node = node_prepare($node);
187 }
188 node_invoke_nodeapi($node, 'view');
189 $content = drupal_render($node->content);
190
191 $content = pdfview_check_images($content);
192 $pdf->SetFont(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN);
193 $pdf = theme('pdfview_html', $pdf, $content);
194 return $pdf;
195 }
196
197 /**
198 * Renders a title
199 * @ingroup themeable
200 */
201 function theme_pdfview_title(&$pdf, $title) {
202 $pdf->SetFont(PDF_FONT_NAME_MAIN, 'B', PDF_FONT_SIZE_MAIN * K_TITLE_MAGNIFICATION);
203 $pdf->SetLineWidth(1);
204 $pdf = theme('pdfview_html', $pdf, $title);
205 $pdf->Ln(5);
206 return $pdf;
207 }
208
209 /**
210 * Renders an author line
211 * @ingroup themeable
212 */
213 function theme_pdfview_author(&$pdf, $author) {
214 $pdf->SetFont(PDF_FONT_NAME_MAIN,'BI', PDF_FONT_SIZE_MAIN);
215 $pdf->MultiCell(0, 5, t('By @author', array('@author' => $author)), 0, 'L', 0);
216 return $pdf;
217 }
218
219 /**
220 * Renders a publication date
221 * @ingroup themeable
222 */
223 function theme_pdfview_published(&$pdf, $timestamp) {
224 $published = format_date($timestamp, 'small');
225 $pdf->SetFont('', '', PDF_FONT_SIZE_MAIN);
226 $pdf->MultiCell(0, 5, t('Published: @date', array('@date' => $published)), 0, 'L', 0);
227 $pdf->Ln(5);
228 return $pdf;
229 }
230
231 /**
232 * Renders an HTML string to PDF
233 * @ingroup themeable
234 */
235 function theme_pdfview_html(&$pdf, $html) {
236 foreach (explode("\n", $html) as $one) {
237 $pdf->writeHTML($one);
238 }
239 return $pdf;
240 }
241
242 /**
243 * Ensures that all images are included with absolute paths
244 */
245 function pdfview_check_images($string) {
246 return preg_replace_callback('/(?<=src=")(.*?)(?=")/', '_pdfview_rewrite_image_url', $string);
247 }
248
249 function _pdfview_rewrite_image_url($matches = array()) {
250 $path = $matches[0];
251 $path = str_replace($GLOBALS['base_url'].'/', '', $path); // removes 'http://www.example.com/'
252 if (base_path() == '/') { // checks if base_path = '/'
253 if ($path[0] == '/') { // run the following function only if the first sign is '/' and remove it
254 $path = substr($path, 1);
255 }
256 }
257 else {
258 $path = str_replace(base_path(), '', $path); // if base_path != '/' we can savely remove base_path
259 }
260 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
261 $q = variable_get('clean_url', 0) ? '' : '?q=';
262 $path = str_replace($q.'system/', '', $path);
263 }
264 return $path;
265 }

  ViewVC Help
Powered by ViewVC 1.1.2