Stirling-Tools/Stirling-PDF · error · Error

Failed to load PDF for native print (${response.status})

Error message

Failed to load PDF for native print (${response.status})

What it means

Thrown by resolvePdfSource() (used by printPdfNatively) when fetching the provided url returns response.ok === false, with the status code interpolated. The PDF bytes could not be retrieved for printing. This only triggers when no File/Blob was passed and a url was used instead.

Source

Thrown at frontend/editor/src/desktop/services/nativePrintService.ts:23

  const cleaned = fileName.replace(/[^A-Za-z0-9._-]+/g, "_");
  if (!cleaned.toLowerCase().endsWith(".pdf")) {
    return `${cleaned || "document"}.pdf`;
  }
  return cleaned || "document.pdf";
}

async function resolvePdfSource(file?: File | Blob, url?: string | null) {
  if (file) {
    return file;
  }

  if (!url) {
    return null;
  }

  const response = await fetch(url);
  if (!response.ok) {
    throw new Error(`Failed to load PDF for native print (${response.status})`);
  }

  return response.blob();
}

export async function printPdfNatively(
  file?: File | Blob,
  url?: string | null,
  fileName = "document.pdf",
) {
  const source = await resolvePdfSource(file, url);
  if (!source) {
    throw new Error("No PDF source available for native print");
  }

  const { tempDir, join } = await import("@tauri-apps/api/path");
  const { remove, writeFile } = await import("@tauri-apps/plugin-fs");

View on GitHub (pinned to 9ef20dcab8)

Solutions

  1. Pass the File/Blob directly to printPdfNatively instead of a URL when the bytes are already in memory (avoids the fetch entirely).
  2. For backend URLs, ensure the request includes the auth token and that the resource still exists.
  3. Re-generate the document/tool output before printing if the URL expired.
  4. Handle the status (parse from message) to distinguish auth (401) from not-found (404).

Example fix

// before
await printPdfNatively(undefined, maybeExpiredUrl);

// after: prefer the in-memory blob
await printPdfNatively(pdfBlob, null, fileName);
Defensive patterns

Strategy: validation

Validate before calling

// prefer the in-memory blob; only fetch when you must
if (file) { await printPdfNatively(file, null, fileName); return; }
if (!url) { notify('No document to print.'); return; }

Type guard

function isPrintLoadError(e: unknown): e is Error {
  return e instanceof Error && /^Failed to load PDF for native print \(\d+\)$/.test(e.message);
}

Try / catch

try { await printPdfNatively(file, url, fileName); }
catch (e) {
  if (isPrintLoadError(e)) { show('Could not load the document for printing.'); return; }
  throw e;
}

Prevention

When it happens

Trigger: printPdfNatively(undefined, url) where the url returns 4xx/5xx: an expired blob/object URL, a backend PDF endpoint returning an error, or a URL that needs auth the fetch didn't include.

Common situations: Printing from a tool-result URL whose blob URL was already revoked; backend returned an error page (HTML) instead of the PDF; the document was deleted server-side before printing.

Related errors


AI-assisted analysis of Stirling-Tools/Stirling-PDF@9ef20dcab8 (2026-08-13). Data as JSON: /api/errors/471fe948b062bde3. Report an issue: GitHub.