A few days ago, I put together a demo of using the OpenAI (ChatGPT) API to programmatically analyze a PDF file. I used the Python language for that demo. But in some development scenarios, especially those that involve Web application, it’s sometimes better to use JavaScript. So, so I refactored my Python demo to JavaScript.
As usual, even though the refactoring should have been simple, there were many details to work out along the way. For example, a few weeks ago (as I write this post), the new OpenAI.responses.create() method and API was introduced to eventually replace the older OpenAI.chat.completions.create(). And the “developer” role replaced the “system” role. And on, and on, and on.
Anyway, I have an existing OpenAI account and a couple of existing keys for use in programs that call the API. I created a PDF document using the first couple of pages of the Wikipedia entry on the planet Venus. (I copy-pasted the text into Microsoft Word and then saved as a digital PDF file).
Here’s an output of one run of my demo:
C:\JavaScript\ChatGPT\QueryPDF: node pdf_files_chatgpt.js Begin ChatGPT PDF file demo Reading file ./venus.pdf The question is: What is an interesting fact about Venus? The answer is: An interesting fact about Venus is that **a day on Venus is longer than its year**. Venus takes about **243 Earth days to complete one rotation** on its axis (a Venusian day), but only about **225 Earth days to orbit the Sun** (a Venusian year). This means that, on Venus, the sun rises and sets only once every Venusian year! Additionally, Venus rotates in the **opposite direction** to most planets in the Solar System (retrograde rotation), Done C:\JavaScript\ChatGPT\QueryPDF:
Quite impressive. Notice that the output is truncated because I set max_output_tokens = 100.
I investigated and noticed that the demo worked with all three types of PDF files: scanned PDF(essentially an image), scanned-OCR PDF (an unusual format), and digital PDF. Behind the scenes, the API, if necessary, does all the OCR and preparation — a lot of work that had to be done manually until as recently as just a few months ago.
I wanted to make sure that the query-PDF-file program was correctly pulling information from the PDF file, rather than using the GPT built-in knowledge of Venus that it learned during training (GPT uses the text of Wikipedia for training). I modified the source PDF file to include the fake sentence, “A little-known fact is that the surface of Venus is pink, not green.” And then a paragraph later I added a fake sentence, “Because Venus soil is pink, Venus is sometimes called the cotton candy planet.” When I reran the query program, the response was:
The answer is: An interesting fact about Venus is that its surface is actually pink, not green. This is a little-known detail, and because of its pink soil, Venus is sometimes called the "cotton candy planet."
So, indeed, the program was pulling from the source PDF document. Very nice.
Weirdly, I assumed the demo code would work with ordinary .txt files, but nope, so that will be the topic of a future investigation.
I think the main takeaway is that AI via the APIs of ChatGPT, Claude, and the others, is changing with blistering speed, which means that the Internet is littered with out-of-date and irrelevant examples (maybe even this one by the time you’re reading it.)

Because the development of the OpenAI API library is evolving so fast, the Internet is already littered with hundreds (perhaps thousands) of obsolete and irrelevant example blog posts and YouTube videos. These now-misleading examples never go away — they just become phantom examples.
I learned to read from comic books. In 1961, the “Phantom Zone” first appeared. Criminals would be placed in the Phantom Zone, sometimes for hundreds of years. This thought sort of terrified me.
Left: Adventure Comics #283 (April 1961). The first appearance of the Phantom Zone.
Center: Superman #157 (November 1962).
Right: Superboy #114 (July 1964).
All three covers by artist Curt Swan, my favorite comic book artist of all time.
Demo program:
// pdf_files_chatgpt.js
// query a PDF file
// node.js v22.16.0
import fs from "fs";
import OpenAI from "openai"; // npm install openai
console.log("\nBegin ChatGPT PDF file demo ");
const key = "sk-proj-_AX7bGTXUwg-qojh2T5Z2CVXrox" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"_TbyJ6Pu3Ex8FqgDOFFjKPpbZ3HsmyLwIcOsIldlZd7npEA";
const fp = "./venus.pdf"; // digital PDF
console.log("\nReading file " + fp.toString());
const oai = new OpenAI({apiKey: key})
const f = await oai.files.create({
file: fs.createReadStream(fp), // key quotes opt.
purpose: "user_data",
});
const question = "What is an interesting fact about Venus?";
console.log("\nThe question is: ");
console.log(question);
const response = await oai.responses.create({
model: "gpt-4.1",
input: [
{
role: "system",
content: "You analyze PDF files",
},
{
role: "user",
content: [
{ type: "input_file", file_id: f.id, },
{ type: "input_text", text: question, },
],
},
],
temperature: 0.2, // not creative
top_p: 1.0, // default large search
max_output_tokens: 100,
});
console.log("\nThe answer is: ");
console.log(response.output_text);
console.log("\nDone ");

.NET Test Automation Recipes
Software Testing
SciPy Programming Succinctly
Keras Succinctly
R Programming
Visual Studio Live
Microsoft MLADS Conference
DevIntersection Conference
Machine Learning Week
Ai4 Conference
G2E Conference
iSC West Conference
You must be logged in to post a comment.