def generate_pdf(self): url = self.get_render_url() if self.license is None: raise Exception("PDFGeneratorView requires a license") try: pdf_reactor = PDFreactor() except: raise PDFReactorNotConfigured('PDFreactor python library path needs to be configured.') pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_FATAL) pdf_reactor.setLicenseKey(str(self.license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) result = pdf_reactor.renderDocumentFromURL(url) # Check if successful if result is None: # Not successful, return 500 raise Exception('Error while rendering PDF: {}'.format( pdf_reactor.getError())) else: # Set the correct header for PDF output response = HttpResponse(result, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename={0}'.format( self.get_filename()) return response
def generate_pdf(self): url = self.get_render_url() if self.license is None: raise Exception("PDFGeneratorView requires a license") try: pdf_reactor = PDFreactor() except: raise PDFReactorNotConfigured( 'PDFreactor python library path needs to be configured.') pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN) pdf_reactor.setLicenseKey(str(self.license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) result = pdf_reactor.renderDocumentFromURL(url) # Check if successful if result is None: # Not successful, return 500 raise Exception('Error while rendering PDF: {}'.format( pdf_reactor.getError())) else: # Set the correct header for PDF output response = HttpResponse(result, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename={0}'.format( self.get_filename()) return response
def display(request, pdf=False): """ display (potentially filtered) html view of the calendar """ form = CalendarFilterForm(request.GET) filtered_events = CFPBCalendarEvent.objects.filter(active=True)\ .order_by('-dtstart') if form.is_valid(): if form.cleaned_data.get('filter_calendar', None): calendars = form.cleaned_data['filter_calendar'] filtered_events = filtered_events.filter(calendar__in=calendars) if form.cleaned_data.get('filter_range_date_gte'): gte = form.cleaned_data['filter_range_date_gte'] filtered_events = filtered_events.filter(dtstart__gte=gte) if form.cleaned_data.get('filter_range_date_lte'): # adding timedelta(days=1) makes the end of the range inclusive lte = form.cleaned_data['filter_range_date_lte']+ timedelta(days=1) filtered_events = filtered_events.filter(dtend__lte=lte) else: import pdb;pdb.set_trace() paginator = PaginatorForSheerTemplates(request, filtered_events, 20) page = int(request.GET.get('page', 1)) if pdf: events = filtered_events template_name = 'about-us/the-bureau/leadership-calendar/print/index.html' paginator = None else: try: events = paginator.page(page) paginator.current_page = page except PageNotAnInteger: # If page is not an integer, deliver first page. events = paginator.page(1) paginator.current_page = 1 except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. events = paginator.page(paginator.num_pages) paginator.current_page = paginator.num_pages template_name='about-us/the-bureau/leadership-calendar/index.html' stats = filtered_events.aggregate(Min('dtstart'), Max('dtend')) range_start = form.cleaned_data.get('filter_range_date_gte') or stats['dtstart__min'] range_end = form.cleaned_data.get('filter_range_date_lte') or stats['dtend__max'] context = {'events':events, 'paginator':paginator, 'form':form, 'range_start': range_start, 'range_end':range_end} if pdf and PDFreactor: license = os.environ.get('PDFREACTOR_LICENSE') stylesheet_url = '/static/css/pdfreactor-fonts.css' pdf_reactor = PDFreactor() pdf_reactor.setBaseURL("%s://%s/" % (request.scheme, request.get_host())) pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN) pdf_reactor.setLicenseKey(str(license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) pdf_reactor.addUserStyleSheet('', '', '', stylesheet_url) template = get_template(template_name) html = template.render(context) html = html.replace(u"\u2018", "'").replace(u"\u2019", "'") try: pdf = pdf_reactor.renderDocumentFromContent(html.encode('utf-8')) response = HttpResponse(pdf, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=cfpb-leadership.pdf' return response except (urllib2.HTTPError, urllib2.URLError, BadStatusLine): pass return render(request, template_name, context )
def generate_pdf(self, query_opts): if self.license is None: raise Exception("PDFGeneratorView requires a license") if settings.DEBUG and PDFreactor is None: return HttpResponse( "PDF Reactor is not configured, can not render %s" % self.get_render_url()) try: pdf_reactor = PDFreactor() except: raise PDFReactorNotConfigured( 'PDFreactor python library path needs to be configured.') pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN) pdf_reactor.setLicenseKey(str(self.license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) pdf_reactor.addUserStyleSheet('', '', '', self.get_stylesheet_url()) url = '{0}?filter_calendar={1}&filter_range_date_gte={2}&filter_range_date_lte={3}'.format( self.get_render_url(), query_opts['filter_calendar'], query_opts['filter_range_date_gte'], query_opts['filter_range_date_lte']) result = pdf_reactor.renderDocumentFromURL(url) # Check if successful if result is None: # Not successful, return 500 raise Exception('Error while rendering PDF: {}'.format( pdf_reactor.getError())) else: # Set the correct header for PDF output response = HttpResponse(result, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename={0}'.format( self.get_filename()) return response
def pdf_response(request, context): template_name = 'about-us/the-bureau/leadership-calendar/print/index.html' license = os.environ.get('PDFREACTOR_LICENSE') stylesheet_url = '/static/css/pdfreactor-fonts.css' pdf_reactor = PDFreactor() pdf_reactor.setBaseURL("%s://%s/" % (request.scheme, request.get_host())) pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN) pdf_reactor.setLicenseKey(str(license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) pdf_reactor.addUserStyleSheet('', '', '', stylesheet_url) template = get_template(template_name) html = template.render(context) html = html.replace(u"\u2018", "'").replace(u"\u2019", "'") try: pdf = pdf_reactor.renderDocumentFromContent(html.encode('utf-8')) response = HttpResponse(pdf, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename=cfpb-leadership.pdf' return response except (urllib2.HTTPError, urllib2.URLError, BadStatusLine): messages.error(request, 'Error Creating PDF', extra_tags='leadership-calendar') return redirect_to_leadership_view()
def pdf_response(request, context): template_name = 'about-us/the-bureau/leadership-calendar/print/index.html' license = os.environ.get('PDFREACTOR_LICENSE') stylesheet_url = '/static/css/pdfreactor-fonts.css' pdf_reactor = PDFreactor() pdf_reactor.setBaseURL("http://localhost/") pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN) pdf_reactor.setLicenseKey(str(license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) pdf_reactor.addUserStyleSheet('', '', '', stylesheet_url) template = get_template(template_name) html = template.render(context) html = html.replace(u"\u2018", "'").replace(u"\u2019", "'") try: pdf = pdf_reactor.renderDocumentFromContent(html.encode('utf-8')) response = HttpResponse(pdf, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename=cfpb-leadership.pdf' return response except (urllib2.HTTPError, urllib2.URLError, BadStatusLine): messages.error(request, 'Error Creating PDF', extra_tags='leadership-calendar') return redirect_to_leadership_view()
def generate_pdf(self, query_opts): if self.license is None: raise Exception("PDFGeneratorView requires a license") if settings.DEBUG and PDFreactor is None: return HttpResponse("PDF Reactor is not configured, can not render %s" % self.get_render_url()) try: pdf_reactor = PDFreactor() except: raise PDFReactorNotConfigured('PDFreactor python library path needs to be configured.') pdf_reactor.setLogLevel(PDFreactor.LOG_LEVEL_WARN) pdf_reactor.setLicenseKey(str(self.license)) pdf_reactor.setAuthor('CFPB') pdf_reactor.setAddTags(True) pdf_reactor.setAddBookmarks(True) pdf_reactor.addUserStyleSheet('', '', '', self.get_stylesheet_url()) url = '{0}?filter_calendar={1}&filter_range_date_gte={2}&filter_range_date_lte={3}'.format( self.get_render_url(), query_opts['filter_calendar'], query_opts['filter_range_date_gte'], query_opts['filter_range_date_lte']) result = pdf_reactor.renderDocumentFromURL(url) # Check if successful if result is None: # Not successful, return 500 raise Exception('Error while rendering PDF: {}'.format( pdf_reactor.getError())) else: # Set the correct header for PDF output response = HttpResponse(result, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename={0}'.format( self.get_filename()) return response
sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib/'))) from PDFreactor import * from time import localtime, strftime, sleep # The content to render fileHandle = open( os.path.abspath( os.path.join(os.path.dirname(__file__), '../../resources/contentPython.html'))) content = fileHandle.read() # Create new PDFreactor instance # pdfReactor = PDFreactor("http://yourServer:9423/service/rest") pdfReactor = PDFreactor("https://cloud.pdfreactor.com/service/rest") # Creates today's date date = strftime("%m/%d/%Y %H:%M:%S %p", localtime()) # Get base URL path path = os.getenv('REQUEST_URI') # If the environment variable was not found if not path: # try this one: path = os.environ['PATH_INFO'] # Create a new PDFreactor configuration object config = { # Specify the input document
sys.path.append( os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib/'))) from PDFreactor import * from time import localtime, strftime # The content to render fileHandle = open( os.path.abspath( os.path.join(os.path.dirname(__file__), '../../resources/contentPython.html'))) content = fileHandle.read() # Create new PDFreactor instance # pdfReactor = PDFreactor("http://yourServer:9423/service/rest") pdfReactor = PDFreactor("https://cloud.pdfreactor.com/service/rest") # Creates today's date date = strftime("%m/%d/%Y %H:%M:%S %p", localtime()) # Get base URL path path = os.getenv('REQUEST_URI') # If the environment variable was not found if not path: # try this one: path = os.environ['PATH_INFO'] # Create a new PDFreactor configuration object config = { # Specify the input document