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.

3
  • what drupal version are you targetting? cck suggests d6 but its not entirely clear! :)
    – Alex Weber
    Commented Apr 19, 2011 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
    Commented Apr 19, 2011 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
    Commented Apr 20, 2011 at 13:09

2 Answers 2

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
0
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...

1
  • 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
    Commented Apr 20, 2011 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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