Skip to content

Instantly share code, notes, and snippets.

@aeris
Created June 3, 2017 16:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeris/5ae9a013a42868f55e853cf71347eab4 to your computer and use it in GitHub Desktop.
Save aeris/5ae9a013a42868f55e853cf71347eab4 to your computer and use it in GitHub Desktop.
Export your remarkjs slides to PDF (require phantomjs and imagemagick)
// Licence : AGPLv3+
"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size, pageWidth, pageHeight;
String.prototype.padLeft = function (length, character) {
return new Array(length - this.length + 1).join(character || '0') + this;
}
if (system.args.length !== 3) {
console.log('Usage: rasterize.js URL dirname');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: 1900, height: 1080 };
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: '1cm' };
} else if (system.args.length > 3 && system.args[3].substr(-2) === "px") {
size = system.args[3].split('*');
if (size.length === 2) {
var 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]);
var 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 () {
var pages = page.evaluate(function() {
return document.querySelectorAll(".remark-slide-container").length - 1;
});
var length = pages.toString().length;
for (var i = 0; i < pages; ++i) {
console.log(i + 1 + " / " + pages);
page.render(output + "/" + i.toString().padLeft(length) + ".png");
page.sendEvent('keypress', page.event.key.Space);
}
phantom.exit();
}, 5000);
}
});
}
#!/usr/bin/env python3
# Licence : AGPLv3+
from tempfile import mkdtemp
import os, sys, signal, os.path, subprocess, threading
from shutil import rmtree
from time import sleep
import http.server
class DirectoryHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, directory, *args, **kwargs):
print(directory)
self.__directory = directory
super().__init__(*args, **kwargs)
def translate_path(self, path):
path = os.path.join(self.__directory, path.lstrip(os.sep))
return path
class DirectoryHTTPServer(http.server.HTTPServer):
def __init__(self, directory, address=("", 5000)):
self.__directory = os.path.abspath(directory)
super().__init__(address,
lambda *args, **kwargs:
DirectoryHTTPRequestHandler(self.__directory, *args, **kwargs))
def serv(self):
self.__thread = threading.Thread(target=self.__serv)
self.__thread.start()
def shutdown(self):
super().shutdown()
self.__thread.join()
def __serv(self):
self.serve_forever()
tmp = mkdtemp(dir=os.getcwd())
try:
talk = sys.argv[1]
server = DirectoryHTTPServer(talk)
server.serv()
try:
sleep(1)
subprocess.run(["phantomjs", "remark2pdf.js", "http://localhost:5000/index.html", tmp], check=True)
finally:
server.shutdown()
subprocess.run(["convert", os.path.join(tmp, "*"), talk + ".pdf"], check=True)
finally:
rmtree(tmp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment