diff --git a/pdfpreview.module b/pdfpreview.module
index 7505feb..08a5019 100644
--- a/pdfpreview.module
+++ b/pdfpreview.module
@@ -164,6 +164,11 @@ function pdfpreview_admin_settings() {
 function _pdfpreview_output_filename($file) {
   if (!is_object($file)) $file = (object) $file;
   $output_dir = file_directory_path() . '/' . variable_get('pdfpreview_pathtoimages', 'pdfpreview');
+  
+  if(!file_exists($output_dir)) {
+    mkdir($output_dir, $_mode = 0775, TRUE);
+  }
+  
   if (variable_get('pdfpreview_filenames', PDFPREVIEW_FILENAMES_MACHINE) == PDFPREVIEW_FILENAMES_HUMAN) {
     $filename = basename($file->filename, '.pdf');
     if (module_exists('transliteration')) {
@@ -270,12 +275,12 @@ function _pdfpreview_create_preview($file) {
   if (!is_array($file)) $file = (array) $file;
   $output_filename = _pdfpreview_output_filename($file);
   // Create preview image using ImageMagick
-  if (!file_exists($output_filename) && function_exists('_imageapi_imagemagick_convert_exec')) {
+  if (file_exists($file['filepath']) && !file_exists($output_filename) && function_exists('_imageapi_imagemagick_convert_exec')) {
     _pdfpreview_convert_first_page($file['filepath'], $output_filename);
   }
   return $output_filename;
 }
 
 function _pdfpreview_realpath($path) {
-  return $_SERVER['DOCUMENT_ROOT'] . base_path() . $path;
+  return realpath("./") . base_path() . $path;
 }
