3

I'm working on updating our current intranet to Drupal.

What I need to be able to do is take a PDF and load it in an iframe/node via Adobe Reader (or similar).

I can get something similar using (I think) CCK and FileViewer, but I'd like to remove the fileviewer wrapper and just have a standard PDF in browser.

Assume IE7/8 and Adobe Reader are installed.

Update:
Custom Formatter Module: Custom Module
Result of Module: enter image description here

Update 2 enter image description here Using an Object per these instructions gives a more controlled block to build on. Ultimately I'll add a "You need Adobe installed from HERE to view THIS PDF" for the small chance of no pdf-viewer installed.

  • what drupal version are you targetting? cck suggests d6 but its not entirely clear! :) – Alex Weber Apr 19 '11 at 19:44
  • The more I think about this the more I realize that if you want to open the file via Adobe Reader inside the browser than its not really a Drupal issue at all. – Alex Weber Apr 19 '11 at 22:39
  • added D7 tag. It's a drupal issue if I'm presenting the pdf via my drupal website. the content (pdf) isn't drupal, but the presentation medium is. I will try "generic file" later today as I have time :) I hope that's all that is needed, because that would rock and solve a big question for me. – WernerCD Apr 20 '11 at 13:09
2

You can do this using an <iframe> element within a custom formatter for a file field.

Field formatters control the output of a field: it's what lets you choose whether to display a generic link, the file name, etc. What you can do is create one that displays the PDF within an <iframe> element.

To do this, you need to (at a minimum) implement two hooks: hook_field_formatter_info() and hook_field_formatter_view():

/**
 * Implements hook_field_formatter_info().
 */
function sandbox_field_formatter_info() {
  $info = array();
  $info['sandbox_formatter'] = array(
    'label' => t('Inline file'),
    'description' => t('Display the file within an iframe.'),
    'field types' => array('file'),
  );
  return $info;
}

/**
 * Implements hook_field_formatter_view().
 */
function sandbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // Create the proper markup for each item within the field instance
  foreach ($items as $delta => $item) {
    $path = file_create_url($item['uri']);
    $element[$delta] = array(
      '#markup' => '<iframe src="' . $path . '"></iframe>',
    );
  }
  return $element;
}

Then you can create a file field, set it to only allow PDF, and choose Inline file as the display format. When you view a node with a PDF, you'll see the PDF within an <iframe> element.

The above is a very basic, minimal example: for an intermediate example that shows how to implement formatter settings, check out Dan Linn's tutorial. Also be sure to check out the relevant API pages, which includes sample implementations even more advanced than Linn's:

As an aside, it should be noted that there are a couple of related modules:

  • Iframe: creates an <iframe> element formatter, but it hasn't been ported to Drupal 7 yet.
  • Custom formatters: claims to allow the creation of custom formatters without needing to create a custom module
  • Question updated with attempt at Sandbox Module. I hope I didn't mis-read the intent. Trying the Custom Formatters module, but not a lot of examples and Token is giving me errors as well. – WernerCD Apr 21 '11 at 19:33
  • @WernerCD looks like your issue with the module is a pretty simple fix: you're missing the opening <?php in sandbox.module. – user7 Apr 21 '11 at 19:49
  • Still very new to most of this :) – WernerCD Apr 21 '11 at 19:59
  • @WernerCD no problem: any file contains only PHP code (like sanbox.module) should always have a <?php opening tag. Also, there's no significance to the name sandbox: that's just what I call my boilerplate testing module. You can replace all instances of the word with something more descriptive to you. – user7 Apr 21 '11 at 20:02
  • now... the error is gone but "inline" doesn't show up in Structure > Content Type > Manage Field / Manage Display. Or am I looking in the wrong place? – WernerCD Apr 21 '11 at 20:42
0

If you want to open the PDF in browser window and Adobe Reader + plugins are installed then just use CCK to implement a FileField and display it as "Generic File". The browser will handle opening it automatically. No Drupal magic or skills required...

  • I have a content, setup as simply a title and a "generic file". The problem is it is simply showing a link to the file - click to open/save. I want the file loaded in the browser. Our current intranet makes heavy use of top menu, side menu and content loaded in the "body", of which a PDF is used most often. I'll post some pictures and do a little more research later today. – WernerCD Apr 20 '11 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.