Last active
December 10, 2015 06:58
-
-
Save martync/4398104 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from datetime import datetime | |
from os.path import abspath, dirname | |
import os | |
import json | |
phantom_script = """ | |
var page = require('webpage').create(); | |
page.open("%s", function (status) { | |
page.render("%s"); | |
phantom.exit(); | |
}); | |
""" | |
def phantomjs_render(html_content, image_path): | |
""" | |
Usage: | |
phantomjs_render( | |
html_content="<html> <head><link rel="stylesheet" src="http://site.com/stylesheets/base.css"></head> <body><h1>Ipsum</h1><p>Lorem</p></body></html>", | |
image_path="/www/medias/images/created/test2.jpeg" | |
) | |
""" | |
tmp_dir = "%s/tmp/" % dirname(abspath(__file__)) | |
key = datetime.now().strftime('%m%d%s%M') | |
# 1 - create HTML file | |
temp_html = '%spython_phantom_%s.html' % (tmp_dir, key) | |
file = open(temp_html, 'w') | |
file.write(html_content) | |
file.close() | |
# 2 - create JS file | |
temp_js = '%spython_phantom_%s.js' % (tmp_dir, key) | |
file = open(temp_js, 'w') | |
file.write(phantom_script % (temp_html, image_path)) | |
file.close() | |
# 3 - execute phantomjs command | |
os.system("phantomjs %s" % temp_js) | |
# 4 - cleanup | |
os.remove(temp_js) | |
os.remove(temp_html) | |
print 'Execution completed.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment