If a scraper, a supplier portal or a sitemap hands you links to PDFs and scanned images every week, you need two things: the text inside those files, and a way to skip the ones you have already processed. Remembering processed URLs is not enough: the same PDF shows up again with a tracking parameter, on a mirror or under a new name, and a file can change behind the same URL.
This post chains four Apify Actors that we publish (disclosure: we are Greenrun, their authors) into one Python function: download every link and hash each file, keep only content that no earlier run has seen, then OCR the images and convert the documents to Markdown. All output below comes from two runs of the pipeline on 22 November 2026, one with this week's list of links and one with next week's.
How it works
- Bulk File Downloader downloads every link and writes one manifest row per link: final URL, type read from the bytes, size, SHA-256 and a signed link to the stored copy. Identical files within one run are stored once.
- Merge & Deduplicate Datasets in only-new mode passes on only the rows whose SHA-256 no earlier run with the same memory name has written. The memory is a key-value store in your Apify account, so your script keeps no state.
- Image to Text OCR reads the new images: plain text, Markdown, and every line with its position and confidence.
- Document to Markdown converts the new PDFs and Office files to Markdown with page markers and tables.
Steps 3 and 4 read the copies stored in step 1, so every file is fetched from its source only once.
The code
You need an Apify account and its API token. Each call waits for its run to finish and returns the run's items; maxTotalChargeUsd caps what a run may cost.
import os
import requests
API = "https://api.apify.com/v2/acts"
HEADERS = {"Authorization": f"Bearer {os.environ['APIFY_TOKEN']}"}
def run(actor, run_input, max_usd):
"""Run an Actor, wait for it to finish (up to 300 s) and return its dataset items."""
response = requests.post(
f"{API}/{actor}/run-sync-get-dataset-items",
params={"maxTotalChargeUsd": max_usd},
headers=HEADERS,
json=run_input,
timeout=360,
)
response.raise_for_status()
return response.json()
def process(links, memory="my-archive"):
# 1. Download every link: one manifest row per link with the type read from the bytes,
# the size, the SHA-256 and a signed link to the stored copy.
manifest = run("greenrun~bulk-file-downloader", {"urls": links}, max_usd=1)
# 2. Keep only files whose content no earlier run with the same memory has seen.
new_files = run("greenrun~dataset-merge-dedup", {
"items": manifest,
"dedupeFields": ["sha256"],
"requiredFields": ["sha256"],
"onlyNew": True,
"memoryName": memory,
}, max_usd=0.1)
# 3. Text: images to OCR, documents to Markdown. Both read the stored copies.
source = {f["fileUrl"]: f["url"] for f in new_files}
images = [f["fileUrl"] for f in new_files if f["category"] == "image"]
documents = [f["fileUrl"] for f in new_files if f["category"] == "document"]
texts = []
if images:
texts += run("greenrun~image-to-text-ocr", {"urls": images}, max_usd=1)
if documents:
texts += run("greenrun~document-to-markdown", {"urls": documents}, max_usd=1)
for item in texts:
item["source"] = source.get(item["url"], item["url"])
return manifest, new_files, texts
Call it with your list and keep the memory name the same from run to run:
manifest, new_files, texts = process(links, memory="my-archive")
for item in texts:
print(item["source"], len(item.get("text") or item.get("markdown") or ""))
texts holds OCR items (with text) and document items (with markdown); source is the original link, added by the function. The synchronous endpoint waits up to 300 seconds and answers 408 if a run takes longer, so for thousands of links start the runs with POST .../runs and pass the downloader's dataset to the dedup Actor in datasetIds instead of the rows.
Week 1: six links
The first list had three RFC PDFs from rfc-editor.org (12 to 16 pages each), two test images from Tesseract's test suite (phototest.tif and eurotext.tif) and one dead link.
- The downloader reported
Done: 5 of 6 files downloaded (0.8 MB; 5 file and 0 extra-mb events); 1 not found (see RUN-REPORT). The dead link cost nothing. - The dedup step:
Done: 5 unique rows written from 6 read; 1 without the required fields (not written). The not-found row has no SHA-256, sorequiredFieldsdrops it. - OCR read both images (mean word confidence 95.5 and 91.3) and Document to Markdown converted all three PDFs.
An OCR item, shortened (source is the field the function adds):
{
"status": "text",
"format": "tiff",
"width": 640,
"height": 480,
"confidence": 95.5,
"wordCount": 60,
"text": "This is a lot of 12 point text to test the\nocr code and see if it works on all types\nof file format.\n\nThe quick brown dog jumped over the\nlazy fox. ...",
"lines": [
{"text": "This is a lot of 12 point text to test the", "confidence": 95.5,
"bbox": {"x": 36, "y": 92, "width": 544, "height": 30}, "page": 1}
],
"billableEvents": {"image": 1},
"source": "https://raw.githubusercontent.com/tesseract-ocr/test/main/testing/phototest.tif"
}
Week 2: three more links, two new files
The second list repeated the six links and added three: RFC 9239 (new), phototest_2.tif (new: the same sentences in another font, so different bytes) and RFC 9309 again with ?utm_source=newsletter. The downloader recognized the tracking-parameter copy by its content (signature in fileUrl shortened):
{
"index": 9,
"status": "duplicate",
"url": "https://www.rfc-editor.org/rfc/rfc9309.pdf?utm_source=newsletter",
"fileUrl": "https://api.apify.com/v2/key-value-stores/vJTzibB1Ta2hbr6QA/records/00001-rfc9309.pdf?signature=...",
"detectedType": "pdf",
"category": "document",
"sizeBytes": 177688,
"sha256": "149bf3e7f8fa21eaab3cfacf425e624cc44aece2547f65b2f43b97d70511ef0d",
"duplicateOf": "https://www.rfc-editor.org/rfc/rfc9309.pdf",
"error": "Same content (SHA-256) as input 1, stored once",
"billableEvents": {"file": 1, "extra-mb": 0}
}
The dedup step then let through only the two new files: Done: 2 unique rows written from 9 read; 6 seen in earlier runs, 1 without the required fields (not written). OCR ran on one image and Document to Markdown on one PDF:
{
"status": "success",
"format": "pdf",
"title": "RFC 9239: Updates to ECMAScript Media Types",
"pageCount": 13,
"tablesFound": 1,
"ocrPages": 0,
"characters": 30832,
"markdown": "<!-- page 1 -->\n\nStream: Internet Engineering Task Force (IETF)\nRFC: 9239\nObsoletes: 4329\n...",
"billableEvents": {"document": 1, "extra-page": 0, "ocr-page": 0}
}
Each run took 3 to 9 seconds; with the API calls, a week's pipeline finished in about 30 seconds (32 and 29 seconds measured).
Skip known links before downloading
The function downloads every link again each week. That catches a file that changed behind the same URL, but every download costs $0.002. For a long list of links that rarely change, drop the known links first with the same dedup Actor, keyed on url and with its own memory name:
def new_links(links, memory="my-archive-links"):
"""Drop links that an earlier run with the same memory already let through."""
rows = run("greenrun~dataset-merge-dedup", {
"items": [{"url": link} for link in links],
"dedupeFields": ["url"],
"onlyNew": True,
"memoryName": memory,
}, max_usd=0.1)
return [row["url"] for row in rows]
fresh = new_links(links)
if fresh:
manifest, new_files, texts = process(fresh)
When it compares links, the dedup Actor ignores http versus https, www., trailing slashes and tracking parameters such as utm_*, gclid and fbclid. With the same two lists it let all 6 links through in week 1 and only the 2 new ones in week 2; the ?utm_source=newsletter link was dropped already at this step. The catch: this gate remembers every link it lets through, including links that fail to download and files that later change behind the same URL. Re-check those separately; the manifest gives failed links a status such as not_found, blocked or error.
Run it every week
The memory lives in your Apify account (a key-value store named dataset-merge-dedup-<memory name>), so the script holds no state: run it from cron, GitHub Actions or any other scheduler, and use a new memory name to start from scratch. Links can also come straight from a scraper's dataset: the downloader and the OCR Actor read them from datasetId and datasetField (for example images.url), next to or instead of urls.
From an AI agent (MCP)
Apify's MCP server can expose just these four Actors as tools:
https://mcp.apify.com?tools=greenrun/bulk-file-downloader,greenrun/dataset-merge-dedup,greenrun/image-to-text-ocr,greenrun/document-to-markdown
Each tool takes the same input fields as above, and the downloader's manifest gives the agent a fileUrl for every stored file to pass to the next tool.
Respectful by default
The downloader and the OCR Actor respect robots.txt, and none of the four Actors uses proxies, logins or CAPTCHA solving. Document to Markdown downloads only the links you give it, here the stored copies. Nothing is collected beyond the files you list.
What it costs
Pay per event, no subscription. Every run starts with a fee of $0.0045 per GB of memory: at the default settings $0.0045 for the downloader and the dedup Actor, $0.009 for the OCR Actor and Document to Markdown. Then:
- Bulk File Downloader: $0.002 per file ($2 per 1,000), plus $0.0002 per MB above 10 MB of one file. A copy with the same content is still downloaded, so it is charged; links that are not found, blocked or filtered are free.
- Merge & Deduplicate Datasets: $0.05 per 1,000 rows written, $0.001 per 1,000 rows read, and $0.0002 per started 100,000 keys of only-new memory (at least once per run in only-new mode).
- Image to Text OCR: $0.002 per image read, with or without text ($2 per 1,000); a multi-page TIFF or an image above 12 megapixels counts more. Files that are not images and failed downloads are free.
- Document to Markdown: $0.005 per document of up to 20 PDF pages ($5 per 1,000), $0.0002 per page above 20, and $0.01 per page that needs OCR. Failed downloads and unsupported files are free.
billableEvents in each item shows what it was charged for. Our two test weeks would have cost $0.0565 and $0.0503, mostly start fees. For a list of 1,000 links with 50 new files a week (30 images under 12 megapixels and 20 PDFs of up to 20 pages, all under 10 MB), the code above costs $2.19 a week, $1.90 of it for downloading the 950 known links again; with the link gate in front it costs $0.30. You can cap any run with maxTotalChargeUsd (at least $0.025; $0.05 for the OCR Actor).
Prices as of 22 November 2026.
When to use something else
- Direct file links only: the downloader does not open web pages to look for links and does not run JavaScript. To collect links from pages first, run a crawler and point the downloader at its dataset.
- The OCR is Tesseract: clean printed text works well (95.5 on
phototest.tifabove), handwriting does not. - Default size limits: 100 MB per downloaded file, 30 MB per image for OCR and 50 MB per document; each is an input setting.
More tools from the same toolbox
The same rules apply to all of them: no proxies, logins or CAPTCHA solving, robots.txt respected, pay per result plus the start fee of $0.0045 per GB of run memory, and each one works as an MCP tool the same way. Prices as of 22 November 2026:
Media files
- Audio and Video Transcription: open-source Whisper (base model) inside the Actor, no API keys; text with timestamps, SRT and VTT, $0.005 per minute of audio. Silent, failed and blocked files are free.
- Video Frame Extractor: frames at exact timestamps, every N seconds, keyframes or scene changes as JPEG, PNG or WebP, from $0.80 per 1,000 frames, plus $0.012 per started block of full decoding (about a minute of video up to 1080p) when a mode needs it, such as scene changes. Broken links and non-video files are free.
- Video & Audio Converter: FFmpeg conversion and compression to MP4 or WebM at 240p to 1080p, video to MP3 and other audio formats, trimming and GIFs; from $0.03 per output minute up to 480p ($0.045 at 720p, $0.08 at 1080p) and $0.002 per minute of output audio. Failed and blocked files are free.
- Image Converter and Compressor: JPEG, PNG, WebP, AVIF, GIF or TIFF output, HEIC input, resizing and EXIF and GPS removal, from $1.50 per 1,000 images ($8.00 per 1,000 as AVIF). Not found, blocked and non-image inputs are free.
Web pages
- Website to Markdown: pages and the PDF and Office files they link to as Markdown, plus llms.txt (our first post), from $2.00 per 1,000 pages and $5.00 per 1,000 documents.
- Website Image Extractor and Downloader: every image on a page with its size and alt text, optional download and ZIP, from $3.00 per 1,000 pages listed and $0.80 per 1,000 images downloaded. Failed pages and pages without images are free.
- Website Screenshot API: full-page or viewport screenshots as PNG, JPEG, WebP or PDF, from $3.00 per 1,000 screenshots. Blocked, missing and unloadable pages are free.
- Bulk URL Status and Redirect Checker: HTTP status, final URL and the full redirect chain for a list of URLs, from $0.30 per 1,000 URLs. URLs disallowed by robots.txt are free.
Questions, or a file that comes out badly? Open an issue on the Issues tab of the Actor's page with the link.
Top comments (0)