{"record":{"id":"471fe948b062bde3","repo":"Stirling-Tools/Stirling-PDF","slug":"failed-to-load-pdf-for-native-print-response-st","errorCode":null,"errorMessage":"Failed to load PDF for native print (${response.status})","messagePattern":"Failed to load PDF for native print \\((.+?)\\)","errorType":"http","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/editor/src/desktop/services/nativePrintService.ts","lineNumber":23,"sourceCode":"  const cleaned = fileName.replace(/[^A-Za-z0-9._-]+/g, \"_\");\n  if (!cleaned.toLowerCase().endsWith(\".pdf\")) {\n    return `${cleaned || \"document\"}.pdf`;\n  }\n  return cleaned || \"document.pdf\";\n}\n\nasync function resolvePdfSource(file?: File | Blob, url?: string | null) {\n  if (file) {\n    return file;\n  }\n\n  if (!url) {\n    return null;\n  }\n\n  const response = await fetch(url);\n  if (!response.ok) {\n    throw new Error(`Failed to load PDF for native print (${response.status})`);\n  }\n\n  return response.blob();\n}\n\nexport async function printPdfNatively(\n  file?: File | Blob,\n  url?: string | null,\n  fileName = \"document.pdf\",\n) {\n  const source = await resolvePdfSource(file, url);\n  if (!source) {\n    throw new Error(\"No PDF source available for native print\");\n  }\n\n  const { tempDir, join } = await import(\"@tauri-apps/api/path\");\n  const { remove, writeFile } = await import(\"@tauri-apps/plugin-fs\");\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/desktop/services/nativePrintService.ts#L5-L41","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the File/Blob directly to printPdfNatively instead of a URL when the bytes are already in memory (avoids the fetch entirely).","For backend URLs, ensure the request includes the auth token and that the resource still exists.","Re-generate the document/tool output before printing if the URL expired.","Handle the status (parse from message) to distinguish auth (401) from not-found (404)."],"exampleFix":"// before\nawait printPdfNatively(undefined, maybeExpiredUrl);\n\n// after: prefer the in-memory blob\nawait printPdfNatively(pdfBlob, null, fileName);","handlingStrategy":"validation","validationCode":"// prefer the in-memory blob; only fetch when you must\nif (file) { await printPdfNatively(file, null, fileName); return; }\nif (!url) { notify('No document to print.'); return; }","typeGuard":"function isPrintLoadError(e: unknown): e is Error {\n  return e instanceof Error && /^Failed to load PDF for native print \\(\\d+\\)$/.test(e.message);\n}","tryCatchPattern":"try { await printPdfNatively(file, url, fileName); }\ncatch (e) {\n  if (isPrintLoadError(e)) { show('Could not load the document for printing.'); return; }\n  throw e;\n}","preventionTips":["Pass the File/Blob directly when available to skip the fetch.","Ensure the URL is still valid (not a revoked blob URL) before printing.","Include auth on protected PDF URLs."],"tags":["print","pdf","network","http-status","desktop"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}