diff --git a/README.md b/README.md new file mode 100644 index 0000000..23b4243 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# pdfcomprezzor + +Simple WASM-Program to compress pdffiles with pictures in browser, based on pdfcpu. The programm replaces all images that are bigger than 1240x1754 are resized to this size and converted to JPG. This is done using pdfcpu. We decode, compress, delete and insert each embedded image. This reuses the object of the original object and thus replaces the image. + +## Build +``` +go mod vendor +``` + Deactivate the init function in `vendor/github.com/pdfcpu/pdfcpu/pkg/font/metrics.go`, otherwise the program can not start in WASM. This is caused initialy by a call to `User.Dir()`. + +``` +GOOS=js GOARCH=wasm go build -o pdfcomprezzor.wasm +``` +## API +### using worker.js + +```js +var l={l:0}; +var worker = new Worker('worker.js'); + +worker.addEventListener('message', function(e) { + console.log('Worker said: ', e); + if(e.data.type=="log"){ + let div = document.createElement( "div"); + div.textContent =e.data.message; + document.querySelector("body").appendChild(div); + } else if (e.data.type=="result"){ + console.log(l); + alert(`TOOK: ${e.data.time}`) + downloadBlob(e.data.result,"smaller.pdf","application/pdf"); + } +}, false); +worker.postMessage({array,l}); +``` +where array is a Uint8Array containing the pdf file. You could load it, via + +```js +var reader = new FileReader(); +reader.onload = function() { + var arrayBuffer = this.result; + array = new Uint8Array(arrayBuffer); + // code from above.... + }; +reader.readAsArrayBuffer(this.files[0]); +``` + +## Running the example + + Serve the files with a server, that supports mime-type `application/wasm` + +Navigate to index.html and open a file, wait for compression... + +License: Apache 2.0 +(c) 2020, Henrik Reinstädtler \ No newline at end of file diff --git a/main.go b/main.go index 5eaae23..024a43d 100644 --- a/main.go +++ b/main.go @@ -91,7 +91,7 @@ func Log(a ...interface{}) { time.Sleep(200 * time.Millisecond) } func main() { - onImgLoadCb := js.FuncOf(func(this js.Value, args []js.Value) interface{} { + onCompress := js.FuncOf(func(this js.Value, args []js.Value) interface{} { LogCallback = args[len(args)-1:][0] Log("called") Log("File size in Bytes:", args[0].Get("length").Int()) @@ -124,11 +124,12 @@ func main() { Log("Image size:", image1.Bounds().Dx(), image1.Bounds().Dy()) if image1.Bounds().Dx() > 1000 { Log("Compress this image.....") - smaller := resize.Resize(uint(image1.Bounds().Dx()/2.0), 0, image1, resize.Lanczos2) + smaller := resize.Thumbnail(1240, 1754, image1, resize.Lanczos2) + //smaller := resize.Resize(uint(image1.Bounds().Dx()/2.0), 0, image1, resize.Lanczos2) var b bytes.Buffer w := bufio.NewWriter(&b) err := jpeg.Encode(w, smaller, nil) - images = append(images, CompressedImage{Image: smaller, ObjNr: objNr}) + images = append(images, CompressedImage{ObjNr: objNr}) if err != nil { Log("Error enconding", err) } @@ -164,7 +165,7 @@ func main() { return len(wr.Bytes()) }) - js.Global().Set("loadImage", onImgLoadCb) + js.Global().Set("compress", onCompress) <-done if false { diff --git a/worker.js b/worker.js index 0007217..5dfb44f 100644 --- a/worker.js +++ b/worker.js @@ -1,6 +1,6 @@ importScripts("wasm_exec.js") -self.addEventListener('message', function(e) { +self.addEventListener('message', function (e) { if (!WebAssembly.instantiateStreaming) { // polyfill WebAssembly.instantiateStreaming = async (resp, importObject) => { const source = await (await resp).arrayBuffer() @@ -11,17 +11,21 @@ self.addEventListener('message', function(e) { WebAssembly.instantiateStreaming(fetch("pdfcomprezzor.wasm"), go.importObject).then((result) => { go.run(result.instance); var a = performance.now(); - loadImage(e.data.array,e.data.l, (err, message) => { - self.postMessage({err,message,type:"log"}) + compress(e.data.array, e.data.l, (err, message) => { + self.postMessage({ + err, + message, + type: "log" + }) }); var b = performance.now(); - let result2 = e.data.array.slice(0,e.data.l.l); - self.postMessage({result:result2,type:"result" ,time:b-a}); + let result2 = e.data.array.slice(0, e.data.l.l); + self.postMessage({ + result: result2, + type: "result", + time: b - a + }); }); console.log(e); console.log(self); - /* loadImage(e.data.array,e.data.l, (err, message) => { - self.postMessage({err,message,type:"log"}) - });*/ - - }, false); \ No newline at end of file +}, false); \ No newline at end of file