See https://www.palantir.com/docs/foundry/functions/api-attachments
One trivial solution is to use Typescript v2 functions (currently experimental at time of writing, which means that you might have compile-time breaks to handle whenever you upgrade the repository, for example).
To do so, create a new code repositoy and select “Functions” > “Typescript v2”
Here is an example code snippets that loads the attachment in memory and print the number of pages of the document.
import { Osdk} from "@osdk/client";
import { PdfMediaObjectExample } from "@osdk/generated";
import { PDFDocument, rgb } from 'pdf-lib';
async function getAttachmentContent(oneObject: Osdk.Instance<PdfMediaObjectExample>): Promise<string> {
const metadata = await oneObject.attachment?.fetchMetadata();
// metadata?.filename
// metadata?.mediaType
// metadata?.rid
// metadata?.sizeBytes
const response = await oneObject.attachment?.fetchContents();
// response?.ok
// response?.text
// response?.arrayBuffer
// response?.body
// response?.bodyUsed
// ...
const blob = await response?.blob()
// Convert the Blob to an ArrayBuffer
const arrayBuffer = await blob!.arrayBuffer();
// Convert the ArrayBuffer to a Uint8Array
const pdfBytes = new Uint8Array(arrayBuffer);
// Load the existing PDF document
const pdfDoc = await PDFDocument.load(pdfBytes);
// Get the first page of the document
const pages = pdfDoc.getPages();
const firstPage = pages[0];
// Extract some information or perform operations on the first page
// For instance, you could return the number of pages or some text
const numberOfPages = pages.length;
return `PDF loaded in memory with ${numberOfPages} pages.`;
}
export default getAttachmentContent;