def _create_image(latex_program, latex_document, base_name, color, **kwargs):
 rel_source_path = (base_name + '.tex')
 pdf_path = os.path.join(temp_path, (base_name + '.pdf'))
 image_path = os.path.join(temp_path, (base_name + _IMAGE_EXTENSION))
 if os.path.exists(pdf_path):
  return
 source_path = os.path.join(temp_path, rel_source_path)
 with open(source_path, 'w', encoding='utf-8') as f:
  f.write(latex_document)
 execute_command([latex_program, '-interaction=nonstopmode', rel_source_path], cwd=temp_path)
 pdf_exists = os.path.exists(pdf_path)
 if (not pdf_exists):
  dvi_path = os.path.join(temp_path, (base_name + '.dvi'))
  if os.path.exists(dvi_path):
   pdf_path = dvi_path
   pdf_exists = True
 if pdf_exists:
  (rc, _, output) = run_ghostscript_command(['-sDEVICE=bbox', '-r72', '-dLastPage=1', pdf_path], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
  if (rc == 0):
   try:
    bbox = [int(x) for x in output.splitlines()[0].lstrip('%%BoundingBox: ').split()]
   except ValueError:
    bbox = None
  else:
   bbox = None
  scale_factor = (8 if _hires else 1)
  command = ['-sDEVICE=pngalpha', '-dLastPage=1', '-sOutputFile={image_path}'.format(image_path=image_path), '-r{density}'.format(density=(_density * scale_factor)), '-dDownScaleFactor={0}'.format(scale_factor), '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4']
  if bbox:
   width = round((((((bbox[2] - bbox[0]) + 4) * _density) * scale_factor) / 72))
   height = round((((((bbox[3] - bbox[1]) + 4) * _density) * scale_factor) / 72))
   command.extend(['-g{width}x{height}'.format(**locals()), '-c', '<</Install {{{0} {1} translate}}>> setpagedevice'.format(((-1) * (bbox[0] - 2)), ((-1) * (bbox[1] - 2))), '-f'])
  command.append(pdf_path)
  run_ghostscript_command(command)
 err_file_path = (image_path + _ERROR_EXTENSION)
 err_log = []
 if (not pdf_exists):
  err_log.append("Failed to run '{latex_program}' to create pdf to preview.".format(**locals()))
  err_log.append('')
  err_log.append('')
  log_file = os.path.join(temp_path, (base_name + '.log'))
  log_exists = os.path.exists(log_file)
  if (not log_exists):
   err_log.append('No log file found.')
  else:
   with open(log_file, 'rb') as f:
    log_data = f.read()
   try:
    (errors, warnings, _) = parse_tex_log(log_data, temp_path)
   except:
    err_log.append('Error while parsing log file.')
    errors = warnings = []
   if errors:
    err_log.append('Errors:')
    err_log.extend(errors)
   if warnings:
    err_log.append('Warnings:')
    err_log.extend(warnings)
   err_log.append('')
  err_log.append('LaTeX document:')
  err_log.append('-----BEGIN DOCUMENT-----')
  err_log.append(latex_document)
  err_log.append('-----END DOCUMENT-----')
  if log_exists:
   err_log.append('')
   log_content = log_data.decode('utf8', 'ignore')
   err_log.append('Log file:')
   err_log.append('-----BEGIN LOG-----')
   err_log.append(log_content)
   err_log.append('-----END LOG-----')
 elif (not os.path.exists(image_path)):
  err_log.append('Failed to convert pdf to png to preview.')
 if err_log:
  with open(err_file_path, 'w') as f:
   f.write('\n'.join(err_log))
 for ext in ['tex', 'aux', 'log', 'pdf', 'dvi']:
  delete_path = os.path.join(temp_path, ((base_name + '.') + ext))
  if os.path.exists(delete_path):
   os.remove(delete_path)

def _create_image ( latex_program , latex_document , base_name , color , ** kwargs ) : rel_source_path = ( base_name + '.tex' ) pdf_path = os . path . join ( temp_path , ( base_name + '.pdf' ) ) image_path = os . path . join ( temp_path , ( base_name + _IMAGE_EXTENSION ) ) if os . path . exists ( pdf_path ) : return source_path = os . path . join ( temp_path , rel_source_path ) with open ( source_path , 'w' , encoding = 'utf-8' ) as f : f . write ( latex_document ) execute_command ( [ latex_program , '-interaction=nonstopmode' , rel_source_path ] , cwd = temp_path ) pdf_exists = os . path . exists ( pdf_path ) if ( not pdf_exists ) : dvi_path = os . path . join ( temp_path , ( base_name + '.dvi' ) ) if os . path . exists ( dvi_path ) : pdf_path = dvi_path pdf_exists = True if pdf_exists : ( rc , _ , output ) = run_ghostscript_command ( [ '-sDEVICE=bbox' , '-r72' , '-dLastPage=1' , pdf_path ] , stdout = subprocess . DEVNULL , stderr = subprocess . PIPE ) if ( rc == 0 ) : try : bbox = [ int ( x ) for x in output . splitlines ( ) [ 0 ] . lstrip ( '%%BoundingBox: ' ) . split ( ) ] except ValueError : bbox = None else : bbox = None scale_factor = ( 8 if _hires else 1 ) command = [ '-sDEVICE=pngalpha' , '-dLastPage=1' , '-sOutputFile={image_path}' . format ( image_path = image_path ) , '-r{density}' . format ( density = ( _density * scale_factor ) ) , '-dDownScaleFactor={0}' . format ( scale_factor ) , '-dTextAlphaBits=4' , '-dGraphicsAlphaBits=4' ] if bbox : width = round ( ( ( ( ( ( bbox [ 2 ] - bbox [ 0 ] ) + 4 ) * _density ) * scale_factor ) / 72 ) ) height = round ( ( ( ( ( ( bbox [ 3 ] - bbox [ 1 ] ) + 4 ) * _density ) * scale_factor ) / 72 ) ) command . extend ( [ '-g{width}x{height}' . format ( ** locals ( ) ) , '-c' , '<</Install {{{0} {1} translate}}>> setpagedevice' . format ( ( ( - 1 ) * ( bbox [ 0 ] - 2 ) ) , ( ( - 1 ) * ( bbox [ 1 ] - 2 ) ) ) , '-f' ] ) command . append ( pdf_path ) run_ghostscript_command ( command ) err_file_path = ( image_path + _ERROR_EXTENSION ) err_log = [ ] if ( not pdf_exists ) : err_log . append ( "Failed to run '{latex_program}' to create pdf to preview." . format ( ** locals ( ) ) ) err_log . append ( '' ) err_log . append ( '' ) log_file = os . path . join ( temp_path , ( base_name + '.log' ) ) log_exists = os . path . exists ( log_file ) if ( not log_exists ) : err_log . append ( 'No log file found.' ) else : with open ( log_file , 'rb' ) as f : log_data = f . read ( ) try : ( errors , warnings , _ ) = parse_tex_log ( log_data , temp_path ) except : err_log . append ( 'Error while parsing log file.' ) errors = warnings = [ ] if errors : err_log . append ( 'Errors:' ) err_log . extend ( errors ) if warnings : err_log . append ( 'Warnings:' ) err_log . extend ( warnings ) err_log . append ( '' ) err_log . append ( 'LaTeX document:' ) err_log . append ( '-----BEGIN DOCUMENT-----' ) err_log . append ( latex_document ) err_log . append ( '-----END DOCUMENT-----' ) if log_exists : err_log . append ( '' ) log_content = log_data . decode ( 'utf8' , 'ignore' ) err_log . append ( 'Log file:' ) err_log . append ( '-----BEGIN LOG-----' ) err_log . append ( log_content ) err_log . append ( '-----END LOG-----' ) elif ( not os . path . exists ( image_path ) ) : err_log . append ( 'Failed to convert pdf to png to preview.' ) if err_log : with open ( err_file_path , 'w' ) as f : f . write ( '\n' . join ( err_log ) ) for ext in [ 'tex' , 'aux' , 'log' , 'pdf' , 'dvi' ] : delete_path = os . path . join ( temp_path , ( ( base_name + '.' ) + ext ) ) if os . path . exists ( delete_path ) : os . remove ( delete_path )
creates vdi from the image stored in the local cache .