Skip to main content
Participant
January 4, 2024
Question

REST PDF Services API not working - Getting 400 code : "CORRUPT_DOCUMENT" message

  • January 4, 2024
  • 1 reply
  • 2976 views

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. 

  1. code: "CORRUPT_DOCUMENT"
  2. message: "The input file appears to be corrupted and cannot be processed.; requestId=fV8oppi4abzgpbGH3qpxxPeRLEMoVNK1"
  3. 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;

 

 

This topic has been closed for replies.

1 reply

Raymond Camden
Community Manager
Community Manager
January 4, 2024

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.

Participant
January 4, 2024

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.

quote

 Maybe you need the content length perhaps.

 

I'm not sure what you mean.