Skip to content

Ghostscript 10 based on https://github.com/jkrimmer/ghostpdl-wasm #7

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 74 additions & 46 deletions src/lib/background-worker.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,98 @@
function loadScript() {
import("./gs-worker.js");
}
import gs from "./gs.js";

var Module;
let Module;

function _GSPS2PDF(
dataStruct,
responseCallback,
progressCallback,
statusUpdateCallback,
) {
// first download the ps data
var xhr = new XMLHttpRequest();
xhr.open("GET", dataStruct.psDataURL);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
console.log('onload')
xhr.onload = async function () {
// release the URL
self.URL.revokeObjectURL(dataStruct.psDataURL);
//set up EMScripten environment
// load module
Module = {
preRun: [
function () {
self.Module.FS.writeFile("input.pdf", new Uint8Array(xhr.response));
},
],
postRun: [
function () {
var uarray = self.Module.FS.readFile("output.pdf", { encoding: "binary" });
var blob = new Blob([uarray], { type: "application/octet-stream" });
var pdfDataURL = self.URL.createObjectURL(blob);
responseCallback({ pdfDataURL: pdfDataURL, url: dataStruct.url });
},
],
arguments: [
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
"-dPDFSETTINGS=/ebook",
"-DNOPAUSE",
"-dQUIET",
"-dBATCH",
"-sOutputFile=output.pdf",
"input.pdf",
],
print: function (text) {},
printErr: function (text) {},
print: function (text) {
statusUpdateCallback(text);
},
printErr: function (text) {
statusUpdateCallback("Error: " + text);
console.error(text);
},
setStatus: function (text) {
if (!Module.setStatus.last)
Module.setStatus.last = {time: Date.now(), text: ""};
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30)
// if this is a progress update, skip it if too soon
return;
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
if (progressCallback)
progressCallback(false, parseInt(m[2]) * 100, parseInt(m[4]) * 100);
} else {
if (progressCallback)
progressCallback(true, 0, 0);
}
if (statusUpdateCallback)
statusUpdateCallback(text);
},
totalDependencies: 0,
noExitRuntime: 1
};
// Module.setStatus("Loading Ghostscript...");
if (!self.Module) {
self.Module = Module;
loadScript();
} else {
self.Module["calledRun"] = false;
self.Module["postRun"] = Module.postRun;
self.Module["preRun"] = Module.preRun;
self.Module.callMain();
}
Module.setStatus("Loading Ghostscript...");
const wasmModule = await gs();
// copy source file to virtual filesystem
const FS = wasmModule.FS;
FS.writeFile = (path, content) => {
const stream = FS.open(path, 'w');
FS.write(stream, new Uint8Array([...content]), 0, content.length, 0);
FS.close(stream);
};
FS.readFile = (path, options = { encoding: 'utf8' }) => {
const stream = FS.open(path, 'r');
const buffer = new Uint8Array(FS.stat(path).size);
FS.read(stream, buffer, 0, buffer.length, 0);
FS.close(stream);

return options.encoding === 'utf8'
? new TextDecoder('utf8').decode(buffer)
: buffer;
};
FS.writeFile("input.pdf", new Uint8Array(xhr.response));
// ghostscript arguments and run ghostscript
var gsargs = [
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.5",
"-dPDFSETTINGS=/ebook",
"-DNOPAUSE",
//"-dQUIET",
"-dBATCH",
"-sOutputFile=output.pdf",
"input.pdf",
]
wasmModule.callMain(gsargs);
// make output file on virtual filesystem downloadable
var uarray = FS.readFile("output.pdf", {encoding: "binary"}); //Uint8Array
var blob = new Blob([uarray], {type: "application/octet-stream"});
var pdfDataURL = self.URL.createObjectURL(blob);
responseCallback({pdfDataURL: pdfDataURL, url: dataStruct.url});
};
xhr.send();
}


self.addEventListener('message', function({data:e}) {
self.addEventListener('message', function ({data: e}) {
console.log("message", e)
// e.data contains the message sent to the worker.
if (e.target !== 'wasm'){
if (e.target !== 'wasm') {
return;
}
console.log('Message received from main script', e.data);
Expand Down
Loading