Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(pdf): raise Fire PDF cap to 30MB and compare raw bytes (#3436)

Abimael Martell994a8b3045c8
diff --git a/apps/api/src/scraper/scrapeURL/engines/pdf/index.ts b/apps/api/src/scraper/scrapeURL/engines/pdf/index.ts
index 1a4e1b09b..e2b2adafd 100644
--- a/apps/api/src/scraper/scrapeURL/engines/pdf/index.ts
+++ b/apps/api/src/scraper/scrapeURL/engines/pdf/index.ts
@@ -21,7 +21,11 @@ import {
} from "../../../../controllers/v2/types";
import type { PDFMode } from "../../../../controllers/v2/types";
import { processPdf, detectPdf } from "@mendable/firecrawl-rs";
-import { MAX_FILE_SIZE, MILLISECONDS_PER_PAGE } from "./types";
+import {
+ FIRE_PDF_MAX_FILE_SIZE,
+ MAX_FILE_SIZE,
+ MILLISECONDS_PER_PAGE,
+} from "./types";
import type { PDFProcessorResult } from "./types";
import {
emitNativeLogs,
@@ -361,7 +365,27 @@ export async function scrapePDF(meta: Meta): Promise<EngineScrapeResult> {
// unless we explicitly routed to MinerU via MINERU_PERCENT.
const skipOCR = rustEnabled && mode === "fast" && !routeToMinerU;
if (!result && !skipOCR) {
- const base64Content = (await readFile(tempFilePath)).toString("base64");
+ const pdfBuffer = await readFile(tempFilePath);
+ const fileSizeBytes = pdfBuffer.length;
+ const base64Content = pdfBuffer.toString("base64");
+
+ if (
+ !forceFirePDF &&
+ !routeToMinerU &&
+ config.FIRE_PDF_ENABLE &&
+ config.FIRE_PDF_BASE_URL &&
+ fileSizeBytes >= FIRE_PDF_MAX_FILE_SIZE
+ ) {
+ meta.logger.warn("PDF skipped by Fire PDF: exceeds size cap", {
+ method: "scrapePDF",
+ event: "pdf_skipped_size",
+ engine: "firepdf",
+ file_size_bytes: fileSizeBytes,
+ max_size_bytes: FIRE_PDF_MAX_FILE_SIZE,
+ scrape_id: meta.id,
+ team_id: meta.internalOptions.teamId,
+ });
+ }
// Route a percentage of traffic to Fire PDF instead of MinerU.
// forceFirePDF always wins; skip percentage-based Fire PDF when
@@ -371,7 +395,7 @@ export async function scrapePDF(meta: Meta): Promise<EngineScrapeResult> {
(!routeToMinerU &&
config.FIRE_PDF_ENABLE &&
config.FIRE_PDF_BASE_URL &&
- base64Content.length < MAX_FILE_SIZE &&
+ fileSizeBytes < FIRE_PDF_MAX_FILE_SIZE &&
Math.random() * 100 < config.FIRE_PDF_PERCENT);
if (useFirePDF) {
@@ -427,7 +451,25 @@ export async function scrapePDF(meta: Meta): Promise<EngineScrapeResult> {
if (
!result &&
!forceFirePDF &&
- base64Content.length < MAX_FILE_SIZE &&
+ fileSizeBytes >= MAX_FILE_SIZE &&
+ config.RUNPOD_MU_API_KEY &&
+ config.RUNPOD_MU_POD_ID
+ ) {
+ meta.logger.warn("PDF skipped by RunPod MU: exceeds size cap", {
+ method: "scrapePDF",
+ event: "pdf_skipped_size",
+ engine: "mineru",
+ file_size_bytes: fileSizeBytes,
+ max_size_bytes: MAX_FILE_SIZE,
+ scrape_id: meta.id,
+ team_id: meta.internalOptions.teamId,
+ });
+ }
+
+ if (
+ !result &&
+ !forceFirePDF &&
+ fileSizeBytes < MAX_FILE_SIZE &&
config.RUNPOD_MU_API_KEY &&
config.RUNPOD_MU_POD_ID
) {
diff --git a/apps/api/src/scraper/scrapeURL/engines/pdf/types.ts b/apps/api/src/scraper/scrapeURL/engines/pdf/types.ts
index d9311cbaf..53d8ac167 100644
--- a/apps/api/src/scraper/scrapeURL/engines/pdf/types.ts
+++ b/apps/api/src/scraper/scrapeURL/engines/pdf/types.ts
@@ -3,4 +3,5 @@ export type PDFProcessorResult = { html: string; markdown?: string };
export type PdfMetadata = { numPages: number; title?: string };
export const MAX_FILE_SIZE = 19 * 1024 * 1024; // 19MB
+export const FIRE_PDF_MAX_FILE_SIZE = 30 * 1024 * 1024; // 30MB
export const MILLISECONDS_PER_PAGE = 150;