Skip to content

Commit

Permalink
Change to resize.Thumbnail, adding readme
Browse files Browse the repository at this point in the history
  • Loading branch information
henrixapp committed Sep 21, 2020
1 parent 6c95f27 commit a5ff7b6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -164,7 +165,7 @@ func main() {
return len(wr.Bytes())
})

js.Global().Set("loadImage", onImgLoadCb)
js.Global().Set("compress", onCompress)
<-done
if false {

Expand Down
24 changes: 14 additions & 10 deletions worker.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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);
}, false);

0 comments on commit a5ff7b6

Please sign in to comment.