I upload a pdf file in the html . I need to send it to a javascript library called "pdf-dist". It extracts text from PDF files. I can't send the PDF file from as parameter to the pdf dist function It always returns an error: format is wrong
The error log:
"Unhandled Rejection (Error): Invalid PDF binary data: either typed array, string, or array-like object is expected in the data property."
I've already tried e.target.value, e.target.files, e.target.files[0] Always returns "format error"
the code:
import React, { Component, useState } from 'react';
import "./upload.css"
const pdfjs = require("pdfjs-dist/legacy/build/pdf")
async function getContent(src){
const doc = await pdfjs.getDocument(src).promise
const page = await doc.getPage(1)
return await page.getTextContent()
}
async function getItems(src){
const content = await getContent(src)
const items = content.items.map((item) => {
alert(item.str)
})
return items
}
function App (){
//const [file, setFile] = useState(null)
var texto = "kkkkkk teste"
async function showFile (e) {
e.preventDefault()
console.log(e.target.files[0])
getItems(e.target.files[0])
}
return (
<div className = "upload">
<h1 className="uploadText">UPLOAD PDF</h1>
<h1 className="uploadText">{texto}</h1>
<div className="uploadWrapper">
<div className="uploadTop">
<input
type="file"
accept='.pdf'
onChange={(e) => showFile(e)}
/>
</div>
</div>
</div>
)
}
export default App;