I have created a custom formatter that reads an attached PDF file, and creates a redered JPG file for display as a preview (Custom formatters ROCK btw) — ( code inspired and ported over from: http://drupal.org/project/pdfpreview ). My only issue is that my site is using "Private Downloads", so the rendered JPG, which is saved in the "private" directory, is not viewable, even to authenticated users.

Anyone have any ideas as to how I can allow users to view the rendered jpg files, without saving them *outside* of the private files dir?

Setup:
- I have a node with a PDF file attached
- The whole site is setup for "Private Downloads"
- I have a Custom Formatter, that creates, and saves a JPG preview of a PDF when the node is displayed
- The JPG preview is saved in the /sites/default/files/private/pdf_preview directory

Problem:
- I am trying to display the created JPG preview file, but even authenticated site visitors can't "see" the image file, because it's in the "private" directory.

Here is the code I'm using in the Custom formatter (you need ImageMagick install on your box for this to work):

if ($variables['#object']->field_asset['und'][0]['filemime'] == 'application/pdf') {
	/* Yes we're dealing with a PDF */
	/* Set and create preview storage directory, if not already exsisting */
	$output_uri = file_default_scheme() . '://pdf_previews';
	$output_dir = drupal_realpath($output_uri);
	
	if (!file_exists($output_dir)) {
		$create_dir = drupal_mkdir($output_dir);
		if (!$create_dir) {
			/* TODO Add error handling if directory can't be created */
			} 	  	
		}  

	/* Set preview filename	*/
	$fid = $variables['#object']->field_asset['und'][0]['fid'];
	$output_filename = $output_dir . '/' . md5('pdf_preview' . $fid) . '.jpg';
	
	/* If this preview does NOT exsist, create one */
	$pdf_uri = drupal_realpath($variables['#object']->field_asset['und'][0]['uri']);
	if (!file_exists($output_filename)) {
		$command = "'" . $pdf_uri . "' -colorspace RGB -geometry 500 -quality '85' '" . $output_filename . "'";
		_imagemagick_convert_exec($command, $output, $error);
	}
	
	$rendered_uri = file_default_scheme() . '://pdf_previews/' . md5('pdf_preview' . $fid) . '.jpg'; 
	$rendered_url = file_create_url($rendered_uri);
	$rendered_image = '<img src="' . $rendered_url . '">';
		
	return $rendered_image;
	
} else {
	print "This is not a PDF";
}

Comments

wOOge’s picture

Issue summary: View changes

adjusted some details

wOOge’s picture

Issue summary: View changes

added credit

deciphered’s picture

Hi wOOge,

Awesome idea for a formatter, I've been flat out the last few months with various projects, including http://customformatters.com, but I'll try to take a look at this tonight as it would make a great formatter to share with others.

I'm currently working on adding the ability for users to contribue formatters on customformatters.com, so if you are interested in helping out with beta testing you can signup at http://customformatters.com/beta-signup

Cheers,
Deciphered.

deciphered’s picture

Issue summary: View changes

updated code snippet

deciphered’s picture

Status: Active » Fixed

So I obviously missed the actual question when I looked at this.

The answer to your question, without testing it out (due to the complexity of reproducing the setup), is likely quite simple; use hook_download() to let anyone access the generated image.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.