PDF-file display
This article is for those who need to display attached PDF document to the page of content view. It can be easily done. You just need to insert fragment of code in the necessary place of output:
Area width and height should be inserted according to page parameters.
If plugin of PDF view is not installed in browser, display will be not available. In order to let the user see the attached file, it's necessary to add link to file in the middle of object tag which will be shown only if view is not possible.
For the output of such region it's possible to write a simple module on the basis of Field API which will create new format of display for field data type. Choice of format is made in Manage Display of content type section.
We'll need 4 hooks for that:
- hook_field_formatter_info() - identifies new format;
- hook_field_formatter_settings_form() - creates form for setting;
- hook_field_formatter_settings_summary() - display of indicated settings;
- hook_field_formatter_view() -field output on content page.
First of all we'll create new format. We'll specify that it will be available for data type file, and it will also contain 3 parameters for setting - width and height of region, and text that will be displayed as a link to download file:
<?php
/**
* Implements hook_field_formatter_info().
*/
function field_pdfdisplay_field_formatter_info() {
return array(
'pdf_formatter' => array(
'label' => t('Display PDF'),
'field types' => array('file'),
'settings' => array(
'pdf_width' => '100%',
'pdf_height' => '450',
'pdf_alt' => t('Download PDF file'),
)
),
);
}
?>Then we'll create elements of form necessary to identify and change above-mentioned parameters:
<?php
/**
* Implements hook_field_formatter_settings_form().
*/
function field_pdfdisplay_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
$element['pdf_width'] = array(
'#type' => 'textfield',
'#title' => t('Object width'),
'#description' => t('Specify the width of the field to display a pdf file (use % or px)'),
'#default_value' => $settings['pdf_width'],
'#required' => TRUE,
);
$element['pdf_height'] = array(
'#type' => 'textfield',
'#title' => t('Object height'),
'#description' => t('Specify the height of the field to display a pdf file (use % or px)'),
'#default_value' => $settings['pdf_height'],
'#required' => TRUE,
);
$element['pdf_alt'] = array(
'#type' => 'textfield',
'#title' => t('Text for download'),
'#description' => t('Specify the text that will appear to download the file'),
'#default_value' => $settings['pdf_alt'],
'#required' => TRUE,
);
return $element;
}
?>Next step is message output where information about our parameters is included:
<?php
/**
* Implements hook_field_formatter_settings_summary().
*/
function field_pdfdisplay_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = t('Region of @width to @height, and text at download link - "@alt"', array(
'@width' => $settings['pdf_width'],
'@height' => $settings['pdf_height'],
'@alt' => $settings['pdf_alt'],
));
return $summary;
}
?>And finally the most important thing - generation of file output:
<?php
/**
* Implements hook_field_formatter_view().
*/
function field_pdfdisplay_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
global $base_url;
$settings = $display['settings'];
$element = array();
$width = $settings['pdf_width'];
$height = $settings['pdf_height'];
$alt = $settings['pdf_alt'];
$path = $base_url . '/' . variable_get('file_public_path') . substr($items[0]['uri'], 8);
$object = '<object width="' . $width . '" height="' . $height . '" type="application/pdf" data="' . $path . '"><a href="' . $path . '">' . $alt . '</a></object>';
$element[0]['#markup'] = $object;
return $element;
}
?>After that we'll have new view format that will display pdf file as a whole. There is also a possibility to configure parameters of region display which allows us to "adjust" it to necessary page size.
For convenience purpose, ready-to-use module for completion of such task is attached.


