Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught SyntaxError: Unexpected token '<' #680

Closed
wasyster opened this issue Nov 8, 2020 · 2 comments
Closed

Uncaught SyntaxError: Unexpected token '<' #680

wasyster opened this issue Nov 8, 2020 · 2 comments
Assignees
Labels
duplicate This issue or pull request already exists question Further information is requested

Comments

@wasyster
Copy link

wasyster commented Nov 8, 2020

HI

I am using react with typescript and I cant get this to work.
I have a form component where I want to display the selected pdf file.
The file got loaded, I got the File variable data. I tried to pass it is as a File to a Document file property, tried it to convert to base64 string, but always got the error:

Uncaught SyntaxError: Unexpected token '<'
Error: Setting up fake worker failed: "Cannot read property 'WorkerMessageHandler' of undefined".

I put here my relevant code

    const [image, setImage] = useState<string>();
    const [imageFile, setImageFile] = useState<File>();

    const [pdfFile, setPdfFile] = useState<File>();
    const [pdf, setPdf] = useState<string>();

    const [pageNumber, setPageNumber] = useState<number>(1);
    const [numPages, setNumPages] = useState<number>();

const onPdfPickHandler = (e: React.FormEvent<HTMLInputElement>): void =>
    {
        if (e.currentTarget.files?.item(0) )
        {
            const file = e.currentTarget.files.item(0);
            setPdfFile(file!);

            fileToPDF(file!);
        }
    }

const fileToPDF = async (data: File): Promise<void> =>
    {
        const reader = new FileReader();
        reader.readAsDataURL(data);
        reader.onload = async () =>
        {
            setPdf(reader.result as string);
        }
    }
{
                                            pdf && <React.Fragment>
                                                <Document file={pdf}
                                                          onLoadSuccess={ (e) => onDocumentLoadSuccess(e)}>
                                                    <Page pageNumber={pageNumber} />
                                                </Document>
                                                {numPages && <p>Page {pageNumber} of {numPages}</p>}
                                            </React.Fragment>
                                        }
@wasyster
Copy link
Author

wasyster commented Nov 8, 2020

this worked for me

import { Document, Page, pdfjs   } from "react-pdf";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

@wojtekmaj
Copy link
Owner

See #97 and others.

@wojtekmaj wojtekmaj self-assigned this Nov 9, 2020
@wojtekmaj wojtekmaj added duplicate This issue or pull request already exists question Further information is requested labels Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants