REST PDF Services API not working - Getting 400 code : "CORRUPT_DOCUMENT" message
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.
- code: "CORRUPT_DOCUMENT"
- message: "The input file appears to be corrupted and cannot be processed.; requestId=fV8oppi4abzgpbGH3qpxxPeRLEMoVNK1"
- status: 400
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;
