My goal is to integrate PDF.js (or Viewer.js) with a Flask application, where I have already retrieved the file from a server.
Instead of using
PDFJS.getDocument('helloworld.pdf')
I prefer to display the downloaded PDF in the browser through an Ajax call. This approach allows the server to have more control over providing access only to authorized users.
For example, in Flask:
@mayapp.route('/files/<int:file_id>', methods=['GET'])
def file_access(file_id: int=None):
// Retrieve file path from the database based on user access
return send_file(file_path)
On the client side:
fetchData: function () {
axios({
method: 'get',
url: '/myapp/files/' + this.file_id,
dataType: ...,
headers: ...,
data: {}
})
.then((response) => {
this.file = response.data
})
}
Next step is to use PDF.js to view the fetched file:
One way is to utilize a base64 encoded string like demonstrated in Flask / postgres - display pdf with PDFJS:
var doc = PDFJS.getDocument({data: pdfData})
However, I am facing challenges in rendering the document properly. Any suggestions?