pdfworker.js 1.0 KB

12345678910111213141516171819202122232425262728
  1. /*
  2. ArozOS Office - PDF worker
  3. ==========================
  4. Assembles a PDF from a display list off the page's thread (see
  5. pdfdraw.js), so a large export does not freeze the editor.
  6. Messages in: { job }
  7. Messages out: { type: "progress", done, total, stage }
  8. { type: "done", bytes } (bytes transferred)
  9. { type: "error", message }
  10. */
  11. /* global importScripts, OfficePdfDraw, fontkit */
  12. importScripts("lib/pdf-lib.min.js", "lib/fontkit.umd.min.js", "pdfdraw.js");
  13. self.onmessage = function (e) {
  14. var job = e.data && e.data.job;
  15. if (!job) return;
  16. OfficePdfDraw.render(job, {
  17. fontkit: self.fontkit,
  18. onProgress: function (done, total, stage) {
  19. self.postMessage({ type: "progress", done: done, total: total, stage: stage });
  20. }
  21. }).then(function (bytes) {
  22. self.postMessage({ type: "done", bytes: bytes }, [bytes.buffer]);
  23. }).catch(function (err) {
  24. self.postMessage({ type: "error", message: (err && err.message) || String(err) });
  25. });
  26. };