1

The PDF Formatter module uses pdftohtml to transform PDF to HTML, and show information.

Changing the module code, it is possible to show images too, but the path used for the image in the HTML markup is wrong.

What can I do to fix it?

The code used by the module is the following.

// PDF to html formatter
case 'file_pdf_to_html_formatter':
  $pdftohtml_installed = pdf_formatter_pdftohtml_installed();
  foreach ($items as $delta => $item) {
    $html = '';
      $html = shell_exec('pdftohtml -q -stdout '.escapeshellarg(drupal_realpath($item['uri'])));
      // pdftohtml tends to generate too many non break spaces,
      // replace them with standard spaces
      $html = str_replace(' ',' ',$html);

      // pdftohtml generates a complete HTML file.
      // Drupal only waits for contents between the body tag
      $html = preg_replace('/.*<BODY[^>]*>(.*)<\/BODY>.*/si', '\\1', $html);
    }

    $element[$delta] = array('#markup' => $html);
  }
  break;

The output I get is the following.

<img src="/var/www/html/drupal/sites/default/files/imgtest.jpg" />

1 Answer 1

1

To get the web-accessible URL for a local file, you should use file_create_url().

$html = shell_exec('pdftohtml -q -stdout '.escapeshellarg(file_create_url($item['uri'])));

Using it would probably not work, anyway: I think that pdftohtml first tries to access the local file to see if it is a PDF file, and it outputs what you see when the file is an image. I doubt that pdftohtml uses <img /> for every file that is not a PDF file or that is not able to access.
In your case, file_create_url('public://imgtest.jpg') would return $base_url/sites/default/files/imgtest.jpg where $base_url is the content of the global $base_url, which includes the http:// or https:// part. While that would be perfect to be used for the <img /> tag, it could not be used as filename, since pdftohtml is not probably able to handle a filename starting with http:// or https://. (In other words, it expects the filename of a local file.)

It is probably easier to check if the file is an image, and output the <img> tag using the output of file_create_url() as image URL in that case, or the output from pdftohtml when it is not an image.

1
  • I am not endorsing the code edit for a module hosted on Drupal.org. I generally think it is a bad idea to edit a module in those cases, if we are not ready to maintain it, which also means merging any bug fixes from the original module to our modified copy.
    – avpaderno
    Commented Oct 10, 2013 at 2:33

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.