<s> import json <EOL> import logging <EOL> from subprocess import Popen , STDOUT , PIPE <EOL> import os <EOL> import phantom_pdf_bin <EOL> import urlparse <EOL> import uuid <EOL> from django . conf import settings <EOL> from django . http import HttpResponse <EOL> logger = logging . getLogger ( __name__ ) <EOL> GENERATE_PDF_JS = os . path . join ( os . path . dirname ( phantom_pdf_bin . __file__ ) , <EOL> '<STR_LIT>' ) <EOL> PHANTOM_ROOT_DIR = '<STR_LIT>' <EOL> DEFAULT_SETTINGS = dict ( <EOL> PHANTOMJS_COOKIE_DIR = os . path . join ( PHANTOM_ROOT_DIR , '<STR_LIT>' ) , <EOL> PHANTOMJS_GENERATE_PDF = GENERATE_PDF_JS , <EOL> PHANTOMJS_PDF_DIR = os . path . join ( PHANTOM_ROOT_DIR , '<STR_LIT>' ) , <EOL> PHANTOMJS_BIN = '<STR_LIT>' , <EOL> PHANTOMJS_FORMAT = '<STR_LIT>' , <EOL> PHANTOMJS_ORIENTATION = '<STR_LIT>' , <EOL> PHANTOMJS_MARGIN = <NUM_LIT:0> , <EOL> KEEP_PDF_FILES = False , <EOL> ) <EOL> class RequestToPDF ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> PHANTOMJS_COOKIE_DIR = None , <EOL> PHANTOMJS_PDF_DIR = None , <EOL> PHANTOMJS_BIN = None , <EOL> PHANTOMJS_GENERATE_PDF = None , <EOL> KEEP_PDF_FILES = None ) : <EOL> """<STR_LIT>""" <EOL> self . PHANTOMJS_COOKIE_DIR = PHANTOMJS_COOKIE_DIR <EOL> self . PHANTOMJS_PDF_DIR = PHANTOMJS_PDF_DIR <EOL> self . PHANTOMJS_BIN = PHANTOMJS_BIN <EOL> self . PHANTOMJS_GENERATE_PDF = PHANTOMJS_GENERATE_PDF <EOL> self . KEEP_PDF_FILES = KEEP_PDF_FILES <EOL> for attr in [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> if getattr ( self , attr , None ) is None : <EOL> value = getattr ( settings , attr , None ) <EOL> if value is None : <EOL> value = DEFAULT_SETTINGS [ attr ] <EOL> setattr ( self , attr , value ) <EOL> assert os . path . isfile ( self . PHANTOMJS_BIN ) , "<STR_LIT>" % self . PHANTOMJS_BIN <EOL> for dir_ in [ self . PHANTOMJS_COOKIE_DIR , self . PHANTOMJS_PDF_DIR ] : <EOL> if not os . path . isdir ( dir_ ) : <EOL> os . makedirs ( dir_ ) <EOL> def _build_url ( self , request ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , path , query , fragment = urlparse . urlsplit ( <EOL> request . build_absolute_uri ( ) ) <EOL> protocol = scheme <EOL> domain = netloc <EOL> return '<STR_LIT>' . format ( <EOL> protocol = protocol , <EOL> domain = domain , <EOL> path = path ) <EOL> def _save_cookie_data ( self , request ) : <EOL> """<STR_LIT>""" <EOL> cookie_file = '<STR_LIT>' . join ( ( <EOL> os . path . join ( <EOL> self . PHANTOMJS_COOKIE_DIR , str ( uuid . uuid1 ( ) ) <EOL> ) , '<STR_LIT>' <EOL> ) ) <EOL> with open ( cookie_file , '<STR_LIT>' ) as fh : <EOL> cookie = '<STR_LIT>' . join ( ( <EOL> request . COOKIES . get ( settings . CSRF_COOKIE_NAME , '<STR_LIT>' ) , <EOL> '<STR_LIT:U+0020>' , <EOL> request . COOKIES . get ( settings . SESSION_COOKIE_NAME , '<STR_LIT>' ) <EOL> ) ) <EOL> fh . write ( cookie ) <EOL> return cookie_file <EOL> def _set_source_file_name ( self , basename = str ( uuid . uuid1 ( ) ) ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( ( os . path . join ( self . PHANTOMJS_PDF_DIR , basename ) , '<STR_LIT>' ) ) <EOL> def _return_response ( self , file_src , basename ) : <EOL> """<STR_LIT>""" <EOL> return_file = None <EOL> with open ( file_src , '<STR_LIT:rb>' ) as f : <EOL> return_file = f . readlines ( ) <EOL> response = HttpResponse ( <EOL> return_file , <EOL> content_type = '<STR_LIT>' <EOL> ) <EOL> content_disposition = '<STR_LIT>' % ( basename ) <EOL> response [ '<STR_LIT>' ] = content_disposition <EOL> if not self . KEEP_PDF_FILES : <EOL> os . remove ( file_src ) <EOL> return response <EOL> def request_to_pdf ( self , request , basename , <EOL> format = None , <EOL> orientation = None , <EOL> margin = None , <EOL> make_response = True ) : <EOL> """<STR_LIT>""" <EOL> format = format or self . PHANTOMJS_FORMAT <EOL> orientation = orientation or self . PHANTOMJS_ORIENTATION <EOL> margin = margin or self . PHANTOMJS_MARGIN <EOL> file_src = self . _set_source_file_name ( basename = basename ) <EOL> try : <EOL> os . remove ( file_src ) <EOL> logger . info ( "<STR_LIT>" , file_src ) <EOL> except OSError : <EOL> pass <EOL> cookie_file = self . _save_cookie_data ( request ) <EOL> url = self . _build_url ( request ) <EOL> domain = urlparse . urlsplit ( <EOL> request . build_absolute_uri ( ) <EOL> ) . netloc . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> ssl_protocol = "<STR_LIT>" <EOL> try : <EOL> phandle = Popen ( [ <EOL> self . PHANTOMJS_BIN , <EOL> ssl_protocol , <EOL> self . PHANTOMJS_GENERATE_PDF , <EOL> url , <EOL> file_src , <EOL> cookie_file , <EOL> domain , <EOL> format , <EOL> orientation , <EOL> json . dumps ( margin ) , <EOL> ] , close_fds = True , stdout = PIPE , stderr = STDOUT ) <EOL> phandle . communicate ( ) <EOL> finally : <EOL> os . remove ( cookie_file ) <EOL> return self . _return_response ( file_src , basename ) if make_response else file_src <EOL> def render_to_pdf ( request , basename , <EOL> format = None , <EOL> orientation = None , <EOL> margin = None , <EOL> make_response = True ) : <EOL> """<STR_LIT>""" <EOL> request2pdf = RequestToPDF ( ) <EOL> response = request2pdf . request_to_pdf ( request , basename , format = format , <EOL> orientation = orientation , margin = margin , make_response = make_response ) <EOL> return response </s>
