-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #703 from hms-dbmi/development
Development
- Loading branch information
Showing
43 changed files
with
1,990 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" ______ __ | ||
____ ____/ / __/ ____ ____ ____ ___ _________ _/ /_____ _____ | ||
/ __ \/ __ / /_ / __ `/ _ \/ __ \/ _ \/ ___/ __ `/ __/ __ \/ ___/ | ||
/ /_/ / /_/ / __/ / /_/ / __/ / / / __/ / / /_/ / /_/ /_/ / / | ||
/ .___/\__,_/_/_____\__, /\___/_/ /_/\___/_/ \__,_/\__/\____/_/ | ||
/_/ /_____/____/ | ||
""" | ||
|
||
__title__ = 'PDF Generator' | ||
__version__ = '0.1.3' | ||
__author__ = 'Charles TISSIER' | ||
__license__ = 'MIT' | ||
__copyright__ = 'Copyright 2017 Charles TISSIER' | ||
|
||
# Version synonym | ||
VERSION = __version__ | ||
|
||
|
||
default_app_config = 'pdf.apps.PdfGeneratorConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.apps import AppConfig | ||
|
||
|
||
class PdfGeneratorConfig(AppConfig): | ||
name = 'pdf' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import subprocess | ||
import os | ||
import random | ||
|
||
from .settings import pdf_settings | ||
from django.http import HttpResponse | ||
from django.core.files.base import ContentFile | ||
|
||
|
||
class PDFGenerator(object): | ||
def __init__(self, html, paperformat='A4', zoom=1, script=pdf_settings.DEFAULT_RASTERIZE_SCRIPT, | ||
temp_dir=pdf_settings.DEFAULT_TEMP_DIR): | ||
self.script = script | ||
self.temp_dir = temp_dir | ||
self.html = html | ||
self.html_file = self.__get_html_filepath() | ||
self.pdf_file = self.__get_pdf_filepath() | ||
self.paperformat = paperformat | ||
self.zoom = zoom | ||
self.pdf_data = None | ||
|
||
self.__write_html() | ||
self.__generate() | ||
self.__set_pdf_data() | ||
self.__remove_source_file() | ||
|
||
def __write_html(self): | ||
with open(self.html_file, 'w') as f: | ||
f.write(self.html) | ||
f.close() | ||
|
||
def __get_html_filepath(self): | ||
return os.path.join(self.temp_dir, '{}.html'.format(PDFGenerator.get_random_filename(25))) | ||
|
||
def __get_pdf_filepath(self): | ||
return os.path.join(self.temp_dir, '{}.pdf'.format(PDFGenerator.get_random_filename(25))) | ||
|
||
def __generate(self): | ||
""" | ||
call the following command: | ||
phantomjs rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] | ||
""" | ||
phantomjs_env = os.environ.copy() | ||
phantomjs_env["OPENSSL_CONF"] = "/etc/openssl/" | ||
command = [ | ||
pdf_settings.PHANTOMJS_BIN_PATH, | ||
'--ssl-protocol=any', | ||
'--ignore-ssl-errors=yes', | ||
self.script, | ||
self.html_file, | ||
self.pdf_file, | ||
self.paperformat, | ||
str(self.zoom) | ||
] | ||
return subprocess.call(command, env=phantomjs_env) | ||
|
||
def __set_pdf_data(self): | ||
with open(self.pdf_file, "rb") as pdf: | ||
self.pdf_data = pdf.read() | ||
|
||
def get_content_file(self, filename): | ||
return ContentFile(self.pdf_data, name=filename) | ||
|
||
def get_data(self): | ||
return self.pdf_data | ||
|
||
def get_http_response(self, filename): | ||
response = HttpResponse(self.pdf_data, content_type='application/pdf') | ||
response['Content-Disposition'] = 'attachment; filename="{}.pdf"'.format(filename) | ||
return response | ||
|
||
def __remove_source_file(self): | ||
html_rm = subprocess.call(['rm', self.html_file]) | ||
pdf_rm = subprocess.call(['rm', self.pdf_file]) | ||
return html_rm & pdf_rm | ||
|
||
@staticmethod | ||
def get_random_filename(nb=50): | ||
choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
return "".join([random.choice(choices) for _ in range(nb)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js | ||
"use strict"; | ||
var page = require('webpage').create(), | ||
system = require('system'), | ||
address, output, size, pageWidth, pageHeight; | ||
|
||
if (system.args.length < 3 || system.args.length > 5) { | ||
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]'); | ||
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); | ||
console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px'); | ||
console.log(' "800px*600px" window, clipped to 800x600'); | ||
phantom.exit(1); | ||
} else { | ||
address = system.args[1]; | ||
output = system.args[2]; | ||
page.viewportSize = { width: 600, height: 600 }; | ||
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") { | ||
size = system.args[3].split('*'); | ||
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } | ||
: { format: system.args[3], orientation: 'portrait', margin: '1.5cm' }; | ||
} else if (system.args.length > 3 && system.args[3].substr(-2) === "px") { | ||
size = system.args[3].split('*'); | ||
if (size.length === 2) { | ||
pageWidth = parseInt(size[0], 10); | ||
pageHeight = parseInt(size[1], 10); | ||
page.viewportSize = { width: pageWidth, height: pageHeight }; | ||
page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight }; | ||
} else { | ||
console.log("size:", system.args[3]); | ||
pageWidth = parseInt(system.args[3], 10); | ||
pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any | ||
console.log ("pageHeight:",pageHeight); | ||
page.viewportSize = { width: pageWidth, height: pageHeight }; | ||
} | ||
} | ||
if (system.args.length > 4) { | ||
page.zoomFactor = system.args[4]; | ||
} | ||
page.open(address, function (status) { | ||
if (status !== 'success') { | ||
console.log('Unable to load the address!'); | ||
phantom.exit(1); | ||
} else { | ||
window.setTimeout(function () { | ||
page.render(output); | ||
phantom.exit(); | ||
}, 200); | ||
} | ||
}); | ||
} |
Oops, something went wrong.