Copy link to clipboard
Copied
I'm currently trying to utilize PDF Services API to collect JSON data, validate the data, use the data to fill a pre-made fillable PDF, and then use the dynamically created PDF with sanitized information.
I have been trying to just get my head wraped around the API services so I built an app to test a few and none of them work, and I keep getting the same error.
I'm sure it's something stupid easy that I just keep overlooking, but I've been at it for an embarassingly long time. Don't judge the code too harshly, I've been throwing everything I can think of at it, and there's some random usless artifacts still floating around.
I'm fairly certain it has to do with the document upload process, but I have no idea what I could be doing differently becuase nothing I've tried has worked. Any insights or suggestions would be greatly appreciated.
import "./App.css";
import React, { useEffect, useState } from "react";
import axios from "axios";
function App() {
const [tryStatus, setTryStatus] = useState(false);
const [pdfStatus, setPdfStatus] = useState();
const [masterToken, setMasterToken] = useState();
const [masterLocation, setMasterLocation] = useState();
const CLIENT = process.env.REACT_APP_PDF_SERVICES_CLIENT_ID;
const SECRET = process.env.REACT_APP_PDF_SERVICES_CLIENT_SECRET;
var statusLoc = "";
var thisAssetID;
var myToken = "";
useEffect(() => {
const fetchData = async () => {
try {
const tokenResponse = await axios.post(
"https://pdf-services.adobe.io/token",
new URLSearchParams({
client_id: CLIENT,
client_secret: SECRET,
})
);
const token = tokenResponse.data.access_token;
setMasterToken(token);
const assetResponse = await axios.post(
"https://pdf-services.adobe.io/assets",
{
mediaType: "application/pdf",
},
{
headers: {
"X-API-Key": CLIENT,
Authorization: token,
"Content-Type": "application/json",
},
}
);
const { assetID, uploadUri } = assetResponse.data;
// Upload the PDF file
await axios.put(uploadUri, new File(["/exportPDFInput"], "mytest"), {
headers: {
"Content-Type": "application/pdf",
},
});
const operationResponse = await axios.post(
"https://pdf-services.adobe.io/operation/exportpdf",
{
assetID: assetID,
targetFormat: "docx",
},
{
headers: {
"x-api-key": CLIENT,
"Content-Type": "application/json",
Authorization: token,
},
}
);
const locationHeader = operationResponse.headers.location;
setMasterLocation(locationHeader);
const statusResponse = await axios.get(locationHeader, {
headers: {
Authorization: token,
"x-api-key": CLIENT,
},
});
console.log(statusResponse);
const statusData = statusResponse.data;
if (statusData.status === "in progress") {
setTryStatus(true);
}
} catch (error) {
console.error("Error:", error);
}
};
fetchData();
}, []);
const tryAgain = async () => {
const statusResponse = await axios.get(masterLocation, {
headers: {
Authorization: masterToken,
"x-api-key": CLIENT,
},
});
setPdfStatus(statusResponse.data.status);
console.log(statusResponse);
};
return (
<div className="App">
<header className="App-header">
<h3>COMPLY OFFICE</h3>
<h1>help</h1>
<button onClick={() => tryAgain(myToken)}>TRY AGAIN</button>
<h2>Status: {pdfStatus}</h2>
</header>
</div>
);
}
export default App;
Copy link to clipboard
Copied
Can you share the PDF?
Also note we don't support: "use the data to fill a pre-made fillable PDF"
Also, I would *not* recommend using our APIs in a client-side application. Folks would be able to steal your client id and token very easily.
Most likely it is the PUT block. Maybe you need the content length perhaps.
Copy link to clipboard
Copied
Hey Raymond, I really appreciate the response.
None of the pdfs I've used have worked. I've tried some of the provided example pdfs, some personal pdfs, and some pdfs hosted online. They all give the same corrupted document error.
Maybe you need the content length perhaps.
I'm not sure what you mean.
Copy link to clipboard
Copied
I've tried all of the attached as well as this link: https://s29.q4cdn.com/175625835/files/doc_downloads/test.pdf
Copy link to clipboard
Copied
I've also tried quite a few iterations of this line:
await axios.put(uploadUri, new File(["/exportPDFInput"], "mytest"),
specifically the new File(["exportPDFInput.pdf"], "mytest.pdf") portion.
Copy link to clipboard
Copied
So, I don't use Axios, I prefer Fetch, but in my Node code (you can find a bunch here: https://github.com/cfjedimaster/document-services-demos), I do this:
async function uploadFile(url, filePath, mediaType) {
let stream = fs.createReadStream(filePath);
let stats = fs.statSync(filePath);
let fileSizeInBytes = stats.size;
let upload = await fetch(url, {
method:'PUT',
redirect:'follow',
headers: {
'Content-Type':mediaType,
'Content-Length':fileSizeInBytes
},
body:stream
});
if(upload.status === 200) return;
else {
throw('Bad result, handle later.');
}
}
Notice how I include a Content-Type and Length header with the PUT request.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now