Fetch PDF

Hi

I am trying to get a buffer of a pdf file contained in the media manager. I think I am finding that the wix fetch api does not support pdf files? Is there another way to do this?

Please post your code.

import { fetch } from ‘wix-fetch’ ;

export async function uploadFile ( ) {
return fetch ( “https://www.mastersswimmingontario.ca/wp-content/uploads/2019/08/MSO-RowansLawConfirmation-Coach.pdf” , { “method” : “get” })
. then ( ( httpResponse ) => {
let url = httpResponse . url ;
let statusCode = httpResponse . status ;
let statusText = httpResponse . statusText ;
let headers = httpResponse . headers ;
let bodyUsed = httpResponse . bodyUsed ;
if ( httpResponse . ok ) {
return httpResponse . json ();
}
else {
return Promise . reject ( “Fetch did not succeed” );
}
} )
. then ( ( json ) => {
return json . body
} )
. catch ( ( err ) => {
console . log ( err );
} );
}

Response:

[backend/pages/waivers2.js, uploadFile] called

invalid json response body at https://www.mastersswimmingontario.ca/wp-content/uploads/2019/08/MSO-RowansLawConfirmation-Coach.pdf reason: Unexpected token % in JSON at position 0

pages/waivers2.js
Line 26

[backend/pages/waivers2.js, uploadFile] returned with undefined

You’re not using it correctly, as the response is not a json.
Are you using it on backend?
What are you trying to do? Let the system read the file stream content? Something else?

Hi J.D.

Thanks for the response. Using it in the backend. I am trying to get a form pdf file I have in the wix media manger and fill out fields using the lib-pdf npm package (the pdf in the above was just an example pdf). I need an arraybuffer as an input to the lib-pdf function (https://www.npmjs.com/package/pdf-lib#fill-form).

Thanks
Bobby

Seems to be a nice NPM-PACKAGE :grin:
And has good DESCRIPTION!