TaTo30 / vue-pdf

PDF component for Vue 3
https://tato30.github.io/vue-pdf/
MIT License
390 stars 58 forks source link

need help: Promise.withResolvers is not a function #127

Closed DidoFitch closed 4 weeks ago

DidoFitch commented 1 month ago

Hello, this issue is not a bug but I need some help.

I‘m using version 1.10.0 of this repository, and in older versions of Chrome (earlier than 105), Promise.withResolvers is not supported, causing an error when I preview PDFs: Promise.withResolvers is not a function.

I tried using Babel to support it or ask ChatGPT, but it didn't work. Can anyone help me?

Snipaste_2024-07-10_09-47-39

I have tried this:

yarn add @babel/core @babel/preset-env @vitejs/plugin-vue vite-plugin-babel -D

yarn add promise-polyfill

vite.config.js:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import viteBabel from 'vite-plugin-babel';

export default defineConfig({
  plugins: [
    vue(),
    viteBabel()
  ]
});

main.js:

import 'promise-polyfill/src/polyfill';
if (!Promise.withResolvers) {
  Promise.withResolvers = function() {
    let resolve, reject;
    const promise = new Promise((res, rej) => {
      resolve = res;
      reject = rej;
    });
    return { promise, resolve, reject };
  };
}
TaTo30 commented 1 month ago

After checking this I found this problem on two points: imagen

The first one is from pdf.js file and can be patched using a polyfill or the code @DidoFitch shared.

The second one is from the pdf.worker.js and cannot be polyfilled surely because the worker is running on different thread and context. In this library the worker is embebed directly: https://github.com/TaTo30/vue-pdf/blob/de24ae408d67cf892cb1e51bf19119056ba4629e/src/components/composable.ts#L2

Using the legacy worker fix the second point but It's still required to add the polyfill for the first point.

import PDFWorker from 'pdfjs-dist/legacy/build/pdf.worker.min?url'
TaTo30 commented 1 month ago

This should works, It's important configure the legacy worker before using usePDF:

<script setup lang="ts">
+ import * as PDFJS from 'pdfjs-dist';
+ import LegacyWorker from 'pdfjs-dist/legacy/build/pdf.worker.min?url';
import { VuePDF, usePDF } from '@tato30/vue-pdf';

+ PDFJS.GlobalWorkerOptions.workerSrc = LegacyWorker

const { pdf } = usePDF('https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf')
</script>

<template>
  <div>
    <VuePDF :pdf="pdf" />
  </div>
</template>
CryptoNinja1210 commented 1 month ago

Hi TaTo! I need you help Could you tell me how can I fix this deployment issue? Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

CryptoNinja1210 commented 1 month ago

I am sorry to border you. I fixed this problem. ` ...

  default_type application/octet-stream;
  include  /etc/nginx/mime.types;
  # include the server config for your application
  include /etc/nginx/app.conf;

  types {
      text/javascript                 js;
      application/javascript          mjs;
  }

... ` I updated nginx.conf file. I leave code of nginx.conf file for other developers Thank you for your time.

ZumelzuR commented 1 month ago

There are PR for this?

TaTo30 commented 4 weeks ago

@ZumelzuR Nope, this issue can be fixed using https://github.com/TaTo30/vue-pdf/issues/127#issuecomment-2227189730

It is not necessary to do something on source code.

ZumelzuR commented 3 weeks ago

@ZumelzuR Nope, this issue can be fixed using #127 (comment)

It is not necessary to do something on source code.

I saw that there are some code in master (that is doing this PDFJS.GlobalWorkerOptions.workerSrc = LegacyWorker) but is there is not a compiled version of this. Would be great have this master changes on a compiled version :)