ExamplesLLM ModelsGenerate Text from PDF

Generate Text from PDF

Some models support the file type. You can use this type to generate text from a PDF file. This example shows how to generate text from a PDF file using the ANTHROPIC provider.

import { Mastra } from '@mastra/core';
import { readFileSync } from 'fs';
import path from 'path';
 
const mastra = new Mastra();
 
const llm = mastra.LLM({
  provider: 'ANTHROPIC',
  name: 'claude-3-5-sonnet-20241022',
});
 
 
const buf = readFileSync(path.join(process.cwd(), './fridge-owners-manual.pdf'))
 
const response = await llm.generate([{
  role: "user",
  content: [
    {
      type: "file",
      mimeType: "application/pdf",
      data: buf
    },
    {
      type: "text",
      text: "Please confirm you can see this PDF file by saying 'YES I can see the PDF' and then tell me what's in it."
    }
  ]
}]);
 
console.log(response.text);





View Example on GitHub

MIT 2025 © Nextra.