Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:senshu/Sozi-export
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume-Savaton-ESEO committed Jan 21, 2020
2 parents be49d7e + edcf4c2 commit c775bb2
Show file tree
Hide file tree
Showing 12 changed files with 2,143 additions and 43 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Options:
* `-r`, `--resolution <number>` Pixels per width/height unit (defaults to 72)
* `-p`, `--paper <size>` A LaTeX paper size (defaults to a4paper)
* `-P`, `--portrait` Set the paper orientation to portrait (disabled by default)
* `-c`, `--png-compression <number>`, Compression level of the generated PNG files (0 to 100, higher means smaller files, defaults to 100)
* `-i`, `--include <list>` Frames to include (defaults to 'all')
* `-x`, `--exclude <list>` Frames to exclude (defaults to 'none')

Expand Down Expand Up @@ -82,6 +83,7 @@ Options:
* `-W`, `--width <number>` Page width (defaults to 29.7)
* `-H`, `--height <number>` Page height (defaults to 21)
* `-r`, `--resolution <number>` Pixels per width/height unit (defaults to 72)
* `-c`, `--png-compression <number>`, Compression level of the generated PNG files (0 to 100, higher means smaller files, defaults to 100)
* `-i`, `--include <list>` Frames to include (defaults to 'all')
* `-x`, `--exclude <list>` Frames to exclude (defaults to 'none')

Expand Down Expand Up @@ -113,6 +115,7 @@ Options:
* `-W`, `--width <number>` Video width, in pixels (defaults to 1024)
* `-H`, `--height <number>` Video height (defaults to 768)
* `-b`, `--bit-rate <number>` Video bit rate (defaults to 2M)
* `-c`, `--png-compression <number>`, Compression level of the generated PNG files (0 to 100, higher means smaller files, defaults to 100)

Known issues and limitations
----------------------------
Expand Down
1 change: 1 addition & 0 deletions cli/sozi-to-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var soziExport = require("../lib");
cmd.option("-o, --output <file>", "Output file")
.option("-W, --width <number>", "Page width (defaults to 29.7)", 29.7)
.option("-H, --height <number>", "Page height (defaults to 21)", 21)
.option("-c, --png-compression <number>", "Compression level of the generated PNG files (0 to 100, higher means smaller files, defaults to 100)", 100)
.option("-r, --resolution <number>", "Pixels per width/height unit (defaults to 72)", 72)
.option("-p, --paper <size>", "A LaTeX paper size (defaults to a4paper)", "a4paper")
.option("-P, --portrait", "Set the paper orientation to portrait (disabled by default)")
Expand Down
1 change: 1 addition & 0 deletions cli/sozi-to-pptx.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var soziExport = require("../lib");
cmd.option("-o, --output <file>", "Output file")
.option("-W, --width <number>", "Page width (defaults to 29.7)", 29.7)
.option("-H, --height <number>", "Page height (defaults to 21)", 21)
.option("-c, --png-compression <number>", "Compression level of the generated PNG files (0 to 100, higher means smaller files, defaults to 100)", 100)
.option("-r, --resolution <number>", "Pixels per width/height unit (defaults to 72)", 72)
.option("-i, --include <list>", "Frames to include (defaults to 'all')", "all")
.option("-x, --exclude <list>", "Frames to exclude (defaults to 'none')", "none")
Expand Down
1 change: 1 addition & 0 deletions cli/sozi-to-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cmd.option("-o, --output <file>", "Output video file, or output folder for an im
.option("-i, --images", "Generate a sequence of PNG image files", false)
.option("-W, --width <number>", "Video width, in pixels (defaults to 1024)", 1024)
.option("-H, --height <number>", "Video height, in pixels (defaults to 768)", 768)
.option("-c, --png-compression <number>", "Compression level of the generated PNG files (0 to 100, higher means smaller files, defaults to 100)", 100)
.option("-b, --bit-rate <number>", "Video bit rate, in bits/second (defaults to 2M)", "2M")
.parse(process.argv);

Expand Down
35 changes: 18 additions & 17 deletions lib/sozi-to-pdf.js → lib/export-frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
var system = require("system");
var fs = require("fs");
var page = require("webpage").create();
var tmpDir;
var tmpDir, compressionLevel;

/*
* Custom implementation of console.log().
* Called from sandboxed Javascript.
*/
page.onConsoleMessage = function (msg) {
console.log("sozi-to-pdf.js> " + msg);
console.log("export-frames.js> " + msg);
};

/*
* Render the current page into a PDF file.
* Render the current page into a PNG file.
* Called from sandboxed Javascript.
*/
page.onCallback = function (index) {
page.render(tmpDir + fs.separator + "img" + index + ".png", {format: "png", quality: 100});
page.render(tmpDir + fs.separator + "img" + index + ".png", {format: "png", quality: 100 - compressionLevel});
};

/*
Expand Down Expand Up @@ -101,7 +101,7 @@ function main(options) {
});

document.querySelector(".sozi-blank-screen").style.display = "none";

frameSelection.forEach(function (frameIsSelected, frameIndex) {
if (frameIsSelected) {
console.log("Exporting frame: " + (frameIndex + 1));
Expand All @@ -111,23 +111,24 @@ function main(options) {
}

if (system.args.length < 7) {
console.log("Usage: sozi-to-pdf.js url.html tmpDir widthPx heightPx incList exclList");
console.log("Usage: export-frames.js url.html tmpDir widthPx heightPx compressionLevel incList exclList");
phantom.exit();
}
else {
var url = system.args[1];
tmpDir = system.args[2];

page.paperSize = {
width: system.args[3] + "px",
height: system.args[4] + "px"
};
var url = system.args[1];
tmpDir = system.args[2];
var width = system.args[3];
var height = system.args[4];
compressionLevel = system.args[5];

// PhantomJS seems to consistently add 16 rows to the rendered images.
page.viewportSize = {
width: parseFloat(system.args[3]),
height: parseFloat(system.args[4])
width: parseFloat(width),
height: parseFloat(height) - 16
};

page.clipRect = {top: 0, left: 0, width: Math.floor(width), height: Math.floor(height)};

page.onInitialized = function () {
page.evaluate(function () {
// PhantomJS 1.9 doesn't support Function.bind
Expand All @@ -142,14 +143,14 @@ else {

page.onLoadFinished = function (status) {
if (status === "success") {
page.evaluate(main, {include: system.args[5], exclude: system.args[6]});
page.evaluate(main, {include: system.args[6], exclude: system.args[7]});
}
phantom.exit();
};

page.open(url, function (status) {
if (status !== "success") {
console.log("sozi-to-pdf.js> Unable to load the document: " + url);
console.log("export-frames.js> Unable to load the document: " + url);
}
});
}
37 changes: 22 additions & 15 deletions lib/sozi-to-video.js → lib/export-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
var system = require("system");
var fs = require("fs");
var page = require("webpage").create();
var tmpDir;
var tmpDir, compressionLevel;
var Timing = require("./Timing");

/*
* Custom implementation of console.log().
* Called from sandboxed Javascript.
*/
page.onConsoleMessage = function (msg) {
console.log("sozi-to-video.js> " + msg);
console.log("export-video.js> " + msg);
};

/*
* Render the current page into a PDF file.
* Called from sandboxed Javascript.
*/
page.onCallback = function (index) {
page.render(tmpDir + fs.separator + "img" + index + ".png", {format: "png", quality: 100});
page.render(tmpDir + fs.separator + "img" + index + ".png", {format: "png", quality: 100 - compressionLevel});
};

/*
Expand All @@ -35,7 +35,13 @@ function main() {
var imageIndex = 0;

document.querySelector(".sozi-blank-screen").style.display = "none";


// Workaround to force the desired image height.
var height = window.innerHeight + "px";
document.body.style.overflow = "hidden";
document.body.style.height = height;
document.body.style.maxHeight = height;

sozi.presentation.frames.forEach(function (currentFrame, currentFrameIndex) {
console.log("Exporting frame: " + (currentFrameIndex + 1));

Expand Down Expand Up @@ -70,23 +76,24 @@ function main() {
}

if (system.args.length < 5) {
console.log("Usage: sozi-to-video.js url.html tmpDir widthPx heightPx");
console.log("Usage: export-video.js url.html tmpDir widthPx heightPx compressionLevel");
phantom.exit();
}
else {
var url = system.args[1];
tmpDir = system.args[2];

page.paperSize = {
width: system.args[3] + "px",
height: system.args[4] + "px"
};
var url = system.args[1];
tmpDir = system.args[2];
var width = system.args[3];
var height = system.args[4];
compressionLevel = system.args[5];

// PhantomJS seems to consistently add 16 rows to the rendered images.
page.viewportSize = {
width: parseFloat(system.args[3]),
height: parseFloat(system.args[4])
width: parseFloat(width),
height: parseFloat(height) - 16
};

page.clipRect = {top: 0, left: 0, width: Math.floor(width), height: Math.floor(height)};

page.onInitialized = function () {
page.evaluate(function () {
// PhantomJS 1.9 doesn't support Function.bind
Expand All @@ -109,7 +116,7 @@ else {

page.open(url, function (status) {
if (status !== "success") {
console.log("sozi-to-video.js> Unable to load the document: " + url);
console.log("export-video.js> Unable to load the document: " + url);
}
});
}
20 changes: 12 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var spawn = require("child_process").spawn;
var glob = require("glob");

var phantom = require('phantomjs-prebuilt');
var soziToPdf = path.join(__dirname, "sozi-to-pdf.js");
var soziToVideo = path.join(__dirname, "sozi-to-video.js");
var soziExportFrames = path.join(__dirname, "export-frames.js");
var soziExportVideo = path.join(__dirname, "export-video.js");

function setOutputFileName(inputFileName, options, ext) {
if (!options.output) {
Expand All @@ -37,8 +37,10 @@ exports.convertToPDF = function (inputFileName, options) {
var tmpDir = tmp.dirSync({unsafeCleanup: true}).name;

// Play the presentation and export each frame to its own file
var p = phantom.exec(soziToPdf, inputFileName, tmpDir,
options.width * options.resolution, options.height * options.resolution,
var p = phantom.exec(soziExportFrames, inputFileName, tmpDir,
options.width * options.resolution,
options.height * options.resolution,
options.pngCompression,
options.include, options.exclude);

p.stdout.pipe(process.stdout);
Expand Down Expand Up @@ -80,8 +82,10 @@ exports.convertToPPTX = function (inputFileName, options) {
var tmpDir = tmp.dirSync({unsafeCleanup: true}).name;

// Play the presentation and export each frame to its own file
var p = phantom.exec(soziToPdf, inputFileName, tmpDir,
options.width * options.resolution, options.height * options.resolution,
var p = phantom.exec(soziExportFrames, inputFileName, tmpDir,
options.width * options.resolution,
options.height * options.resolution,
options.pngCompression,
options.include, options.exclude);

p.stdout.pipe(process.stdout);
Expand Down Expand Up @@ -120,9 +124,9 @@ exports.convertToVideo = function (inputFileName, options) {
var tmpDir = options.images ? options.output : tmp.dirSync({unsafeCleanup: true}).name;

// Play the presentation and export each animation step to its own file
var p = phantom.exec(soziToVideo,
var p = phantom.exec(soziExportVideo,
inputFileName, tmpDir,
options.width, options.height);
options.width, options.height, options.pngCompression);

p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
Expand Down
Loading

0 comments on commit c775bb2

Please sign in to comment.