Skip to content

Commit

Permalink
added tiff preprocessor to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
VolodymyrBaydalka committed May 5, 2024
1 parent e8d6f15 commit 22430b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
16 changes: 16 additions & 0 deletions demo/tiff-preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
async function preprocessTiff(blob) {
let zip = await JSZip.loadAsync(blob);
const tiffs = zip.file(/[.]tiff?$/);

if (tiffs.length == 0)
return blob;

for (let f of tiffs) {
const buffer = await f.async("uint8array");
const tiff = new Tiff({ buffer });
const blob = await new Promise(res => tiff.toCanvas().toBlob(blob => res(blob), "image/png"));
zip.file(f.name, blob);
}

return await zip.generateAsync({ type: "blob" });
}
21 changes: 14 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<!--thumbnail example-->
<script src="./demo/thumbnail.example.js"></script>
<link href="./demo/thumbnail.example.css" rel="stylesheet">
<script crossorigin src="https://unpkg.com/tiff.js@1.0.0/tiff.min.js"></script>
<script src="./demo/tiff-preprocessor.js"></script>
</head>

<body class="vh-100 d-flex flex-column">
Expand Down Expand Up @@ -55,25 +57,30 @@
let currentDocument = null;
const docxOptions = Object.assign(docx.defaultOptions, {
debug: true,
experimental: true
experimental: true,
processMedia: (...args) => {
console.log(args);
return args[0];
}
});

const container = document.querySelector("#document-container");
const fileInput = document.querySelector("#files");
const loadButton = document.querySelector("#loadButton");
const testDocuments = document.querySelector("#testDocuments");

function renderDocx(file) {
async function renderDocx(file) {
currentDocument = file;

if (!currentDocument)
return;

docx.renderAsync(currentDocument, container, null, docxOptions)
.then((x) => {
renderThumbnails(container, document.querySelector("#thumbnails-container"));
console.log(x);
});
let blob = preprocessTiff(currentDocument);
let doc = await docx.parseAsync(blob, docxOptions);
await docx.renderDocument(doc, container, null, docxOptions);

renderThumbnails(container, document.querySelector("#thumbnails-container"));
console.log(doc);
}

fileInput.addEventListener("change", ev => {
Expand Down

0 comments on commit 22430b1

Please sign in to comment.