Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

22.4 KB
Raw Blame
import {
  openRawDocumentSafe,
  closeRawDocument,
  getPdfiumModule,
  PdfiumOpenError,
  FPDF_ERR_PASSWORD,
} from "@editor/services/pdfiumService";
import {
  renderPdfiumPageDataUrl,
  readPdfiumPageMetadata,
} from "@editor/utils/pdfiumPageRender";
import { jpegExifOrientation } from "@editor/utils/jpegOrientation";
import { getDocumentBytes } from "@editor/services/documentBytesCache";
import { getPdfAccess } from "@editor/services/pdfAccessService";
import { looksEncryptedFromTrailer } from "@editor/utils/pdfEncryptionProbe";
import { isDamagedPdfError } from "@editor/utils/pdfDamage";
import { LARGE_PDF_PARSE_LIMIT } from "@editor/utils/pdfParseLimit";

export interface ThumbnailWithMetadata {
  thumbnail: string; // Always returns a thumbnail (placeholder if needed)
  pageCount: number;
  pageRotations?: number[]; // Rotation for each page (0, 90, 180, 270)
  pageDimensions?: Array<{ width: number; height: number }>;
  isEncrypted?: boolean;
  /** PDFium could not parse the whole file; never set from a large-file prefix. */
  isDamaged?: boolean;
}

/**
 * Calculate thumbnail scale based on file size (modern 2024 scaling)
 */
export function calculateScaleFromFileSize(fileSize: number): number {
  const MB = 1024 * 1024;
  if (fileSize < 10 * MB) return 1.0; // Full quality for small files
  if (fileSize < 50 * MB) return 0.8; // High quality for common file sizes
  if (fileSize < 200 * MB) return 0.6; // Good quality for typical large files
  if (fileSize < 500 * MB) return 0.4; // Readable quality for large but manageable files
  return 0.3; // Still usable quality, not tiny
}

/** Callers still get a placeholder, but log the cause: an empty thumbnail is
 *  indistinguishable from "no raster preview", so an outage hides as a nicety. */
function reportThumbnailFailure(file: File, error: unknown): void {
  console.warn(`Thumbnail generation failed for ${file.name}:`, error);
}

export { LARGE_PDF_PARSE_LIMIT };

/** Linearized PDFs keep page 1 + hint tables in the first bytes, so a small
 * prefix is often enough to render a thumbnail without reading the file. */
const LINEARIZED_PREFIX_BYTES = 2 * 1024 * 1024;

/** Longest side of an image thumbnail, in pixels (aspect preserved); the
 * hover preview renders at 150 CSS px, so this covers 2x displays. */
const IMAGE_THUMBNAIL_MAX_SIZE = 320;

/** Bytes read from the head of an image to locate its intrinsic size. */
const IMAGE_HEADER_PROBE_BYTES = 256 * 1024;

/** Extra bytes read when a JPEG's start-of-frame marker sits behind a large
 *  metadata segment; the first probe covers the common case. */
const IMAGE_HEADER_PROBE_LIMIT = 4 * 1024 * 1024;

/** Pages read between yields while collecting whole-document metadata. */
const METADATA_YIELD_INTERVAL = 50;

/**
 * Fills rotation and dimensions for pages 1..pageCount-1. Each read is
 * synchronous WASM on the main thread, so this yields every
 * {@link METADATA_YIELD_INTERVAL} pages to let a frame through on long
 * documents.
 */
async function collectAllPageMetadata(
  docPtr: number,
  pageCount: number,
  pageRotations: number[],
  pageDimensions: Array<{ width: number; height: number }>,
): Promise<void> {
  for (let i = 1; i < pageCount; i++) {
    if (i % METADATA_YIELD_INTERVAL === 0) {
      await new Promise((resolve) => setTimeout(resolve, 0));
    }
    const meta = await readPdfiumPageMetadata(docPtr, i);
    if (!meta) continue;
    pageRotations[i] = meta.rotation;
    pageDimensions[i] = { width: meta.width, height: meta.height };
  }
}

interface PdfiumRenderResult {
  thumbnail: string;
  pageCount: number;
  pageRotations: number[];
  pageDimensions: Array<{ width: number; height: number }>;
  /** Set when the document is password-protected — caller substitutes the
   * encrypted placeholder. Thumbnail/metadata fields are empty in that case. */
  isEncrypted?: boolean;
}

/**
 * Open a PDF with PDFium, render page 1 to a data URL, and optionally
 * collect rotation + dimensions for every page. Returns `isEncrypted: true`
 * (without rendering) when the document is password-protected.
 *
 * @param applyRotation When true, bakes the page's own rotation into the
 *   bitmap (static display). When false, renders upright so callers can
 *   apply rotation via CSS (PageEditor).
 * @param collectAllPagesMetadata When true, reads per-page rotation and
 *   dimensions for all pages. When false (very large files), only the
 *   first page's metadata is populated.
 */
async function renderPdfThumbnailPdfium(
  data: ArrayBuffer,
  scale: number,
  applyRotation: boolean,
  collectAllPagesMetadata: boolean,
  password?: string,
): Promise<PdfiumRenderResult> {
  const m = await getPdfiumModule();
  let docPtr: number;
  try {
    docPtr = await openRawDocumentSafe(data, password);
  } catch (error) {
    if (error instanceof PdfiumOpenError && error.code === FPDF_ERR_PASSWORD) {
      return {
        thumbnail: "",
        pageCount: 1,
        pageRotations: [],
        pageDimensions: [],
        isEncrypted: true,
      };
    }
    throw error;
  }

  try {
    const pageCount = m.FPDF_GetPageCount(docPtr);
    const thumbnail = await renderPdfiumPageDataUrl(docPtr, 0, scale, {
      applyRotation,
    });
    if (!thumbnail) throw new Error("PDFium: failed to render page 0");

    // Page 0 metadata is already available via the render, but read it
    // directly for consistency with the later per-page loop.
    const firstMeta = await readPdfiumPageMetadata(docPtr, 0);
    const pageRotations: number[] = [firstMeta?.rotation ?? 0];
    const pageDimensions: Array<{ width: number; height: number }> = [
      {
        width: firstMeta?.width ?? 0,
        height: firstMeta?.height ?? 0,
      },
    ];

    if (collectAllPagesMetadata) {
      await collectAllPageMetadata(
        docPtr,
        pageCount,
        pageRotations,
        pageDimensions,
      );
    }

    return { thumbnail, pageCount, pageRotations, pageDimensions };
  } finally {
    await closeRawDocument(docPtr);
  }
}

/**
 * Render both thumbnail variants (upright + rotation-baked) from a single
 * document open - halves the parse and memory cost of the add-files path.
 */
async function renderPdfThumbnailPairPdfium(
  data: ArrayBuffer,
  scale: number,
  collectAllPagesMetadata: boolean,
  password?: string,
): Promise<{ unrotated: PdfiumRenderResult; rotated: PdfiumRenderResult }> {
  const m = await getPdfiumModule();
  let docPtr: number;
  try {
    docPtr = await openRawDocumentSafe(data, password);
  } catch (error) {
    if (error instanceof PdfiumOpenError && error.code === FPDF_ERR_PASSWORD) {
      const encrypted: PdfiumRenderResult = {
        thumbnail: "",
        pageCount: 1,
        pageRotations: [],
        pageDimensions: [],
        isEncrypted: true,
      };
      return { unrotated: encrypted, rotated: { ...encrypted } };
    }
    throw error;
  }

  try {
    const pageCount = m.FPDF_GetPageCount(docPtr);
    const unrotatedThumb = await renderPdfiumPageDataUrl(docPtr, 0, scale, {
      applyRotation: false,
    });
    const rotatedThumb = await renderPdfiumPageDataUrl(docPtr, 0, scale, {
      applyRotation: true,
    });
    if (!unrotatedThumb || !rotatedThumb) {
      throw new Error("PDFium: failed to render page 0");
    }

    const firstMeta = await readPdfiumPageMetadata(docPtr, 0);
    const pageRotations: number[] = [firstMeta?.rotation ?? 0];
    const pageDimensions: Array<{ width: number; height: number }> = [
      { width: firstMeta?.width ?? 0, height: firstMeta?.height ?? 0 },
    ];
    if (collectAllPagesMetadata) {
      await collectAllPageMetadata(
        docPtr,
        pageCount,
        pageRotations,
        pageDimensions,
      );
    }

    const base = { pageCount, pageRotations, pageDimensions };
    return {
      unrotated: { thumbnail: unrotatedThumb, ...base },
      rotated: { thumbnail: rotatedThumb, ...base },
    };
  } finally {
    await closeRawDocument(docPtr);
  }
}

async function generatePDFThumbnail(
  arrayBuffer: ArrayBuffer,
  scale: number,
  password?: string,
): Promise<string> {
  const result = await renderPdfThumbnailPdfium(
    arrayBuffer,
    scale,
    true,
    false,
    password,
  );
  if (result.isEncrypted) {
    return "";
  }
  return result.thumbnail;
}

/**
 * Read intrinsic pixel dimensions from an image header without decoding it.
 * Returns null for formats the probe does not recognise, so the caller keeps
 * the width-only resize request instead of failing.
 */
async function readImageDimensions(
  file: File,
): Promise<{ width: number; height: number } | null> {
  let bytes: Uint8Array;
  try {
    bytes = new Uint8Array(
      await file.slice(0, IMAGE_HEADER_PROBE_BYTES).arrayBuffer(),
    );
  } catch {
    return null;
  }
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);

  // PNG: IHDR is the first chunk; big-endian width/height at offsets 16/20.
  if (
    bytes.length >= 24 &&
    bytes[0] === 0x89 &&
    bytes[1] === 0x50 &&
    bytes[2] === 0x4e &&
    bytes[3] === 0x47
  ) {
    return { width: view.getUint32(16), height: view.getUint32(20) };
  }

  // GIF: logical screen descriptor; little-endian width/height at offsets 6/8.
  if (
    bytes.length >= 10 &&
    bytes[0] === 0x47 &&
    bytes[1] === 0x49 &&
    bytes[2] === 0x46
  ) {
    return { width: view.getUint16(6, true), height: view.getUint16(8, true) };
  }

  // BMP: BITMAPINFOHEADER; signed little-endian at 18/22, negative = top-down.
  if (bytes.length >= 26 && bytes[0] === 0x42 && bytes[1] === 0x4d) {
    return {
      width: Math.abs(view.getInt32(18, true)),
      height: Math.abs(view.getInt32(22, true)),
    };
  }

  // WebP: RIFF container, the chunk id at offset 12 identifies the encoding.
  if (
    bytes.length >= 30 &&
    bytes[0] === 0x52 &&
    bytes[1] === 0x49 &&
    bytes[2] === 0x46 &&
    bytes[3] === 0x46 &&
    bytes[8] === 0x57 &&
    bytes[9] === 0x45 &&
    bytes[10] === 0x42 &&
    bytes[11] === 0x50
  ) {
    const chunk = String.fromCharCode(
      bytes[12],
      bytes[13],
      bytes[14],
      bytes[15],
    );
    if (chunk === "VP8X") {
      return {
        width: 1 + (bytes[24] | (bytes[25] << 8) | (bytes[26] << 16)),
        height: 1 + (bytes[27] | (bytes[28] << 8) | (bytes[29] << 16)),
      };
    }
    if (chunk === "VP8 ") {
      return {
        width: view.getUint16(26, true) & 0x3fff,
        height: view.getUint16(28, true) & 0x3fff,
      };
    }
    if (chunk === "VP8L") {
      const bits = view.getUint32(21, true);
      return {
        width: (bits & 0x3fff) + 1,
        height: ((bits >> 14) & 0x3fff) + 1,
      };
    }
    return null;
  }

  // JPEG: walk the segment list until a start-of-frame marker carries the size.
  // A large EXIF/ICC segment can push that marker past the first probe, so the
  // window doubles until it is found or the cap is reached.
  if (bytes.length >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8) {
    const limit = Math.min(file.size, IMAGE_HEADER_PROBE_LIMIT);
    for (let end = bytes.length; ; end = Math.min(end * 2, limit)) {
      const window =
        end === bytes.length
          ? bytes
          : new Uint8Array(await file.slice(0, end).arrayBuffer());
      const dimensions = readJpegDimensions(
        window,
        new DataView(window.buffer, window.byteOffset, window.byteLength),
      );
      if (dimensions) {
        // EXIF orientations 5-8 rotate the image, so the bitmap
        // createImageBitmap produces has the header's axes swapped.
        const orientation = jpegExifOrientation(window);
        return orientation >= 5 && orientation <= 8
          ? { width: dimensions.height, height: dimensions.width }
          : dimensions;
      }
      if (end >= limit) return null;
    }
  }

  return null;
}

/** Walk JPEG segments to the start-of-frame marker that carries the size. */
function readJpegDimensions(
  bytes: Uint8Array,
  view: DataView,
): { width: number; height: number } | null {
  let offset = 2;
  while (offset + 9 <= bytes.length) {
    if (bytes[offset] !== 0xff) return null;
    const marker = bytes[offset + 1];
    if (marker === 0xff) {
      offset += 1;
      continue;
    }
    // Standalone markers have no length field.
    if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8)) {
      offset += 2;
      continue;
    }
    if (marker === 0xd9) return null;
    const length = view.getUint16(offset + 2);
    if (length < 2) return null;
    const isStartOfFrame =
      marker >= 0xc0 &&
      marker <= 0xcf &&
      marker !== 0xc4 &&
      marker !== 0xc8 &&
      marker !== 0xcc;
    if (isStartOfFrame) {
      return {
        height: view.getUint16(offset + 5),
        width: view.getUint16(offset + 7),
      };
    }
    offset += 2 + length;
  }
  return null;
}

/** Resize a known source to fit a `maxSize` box, longest side first, never
 *  enlarging it. */
function thumbnailResizeOptions(
  source: { width: number; height: number },
  maxSize: number,
): {
  resizeWidth: number;
  resizeHeight: number;
} {
  const scale = Math.min(1, maxSize / Math.max(source.width, source.height));
  return {
    resizeWidth: Math.max(1, Math.round(source.width * scale)),
    resizeHeight: Math.max(1, Math.round(source.height * scale)),
  };
}

function paintThumbnail(
  ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
  bitmap: ImageBitmap,
): void {
  ctx.fillStyle = "#ffffff";
  ctx.fillRect(0, 0, bitmap.width, bitmap.height);
  ctx.drawImage(bitmap, 0, 0);
}

function blobToDataUrl(blob: Blob): Promise<string> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => resolve(reader.result as string);
    reader.onerror = () => reject(reader.error);
    reader.readAsDataURL(blob);
  });
}

/** JPEG-encode a bitmap. OffscreenCanvas keeps the encode off the main thread
 *  where the engine supports it; jsdom and older WebKit fall back to a DOM
 *  canvas. */
async function encodeThumbnailJpeg(bitmap: ImageBitmap): Promise<string> {
  if (typeof OffscreenCanvas === "function") {
    const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
    const ctx = canvas.getContext("2d");
    if (!ctx) throw new Error("2d context unavailable");
    paintThumbnail(ctx, bitmap);
    // JPEG on purpose: the data URL is the thumbnail contract every consumer
    // and the DOM fallback below share, so the engine must not pick another
    // format here.
    const blob = await canvas.convertToBlob({
      type: "image/jpeg",
      quality: 0.8,
    });
    return blobToDataUrl(blob);
  }

  const canvas = document.createElement("canvas");
  canvas.width = bitmap.width;
  canvas.height = bitmap.height;
  const ctx = canvas.getContext("2d");
  if (!ctx) throw new Error("2d context unavailable");
  paintThumbnail(ctx, bitmap);
  return canvas.toDataURL("image/jpeg", 0.8);
}

/**
 * A JPEG data URL of the image with its longest side at most `maxSize` pixels,
 * decoded at that size rather than at full resolution. Throws when the header
 * gives no dimensions or the browser cannot decode the image.
 */
export async function generateImageThumbnail(
  file: File,
  maxSize: number,
): Promise<string> {
  const source = await readImageDimensions(file);
  // Without a header ratio a width-only request would let an extreme portrait
  // ask for a maxSize x tens-of-millions bitmap.
  if (!source) throw new Error("image dimensions unavailable");
  const bitmap = await createImageBitmap(file, {
    ...thumbnailResizeOptions(source, maxSize),
    resizeQuality: "high",
    imageOrientation: "from-image",
  });
  try {
    return await encodeThumbnailJpeg(bitmap);
  } finally {
    bitmap.close();
  }
}

/**
 * Generate thumbnail for any file type - always returns a thumbnail (placeholder if needed)
 */
export async function generateThumbnailForFile(file: File): Promise<string> {
  // Very large PDFs skip thumbnail generation — SVG icon shown in UI instead
  if (file.size >= LARGE_PDF_PARSE_LIMIT) {
    return "";
  }

  // Handle image files - decode once at tooltip size instead of full-res.
  // A phone photo decodes to ~48 MB RGBA plus an ~11 MB data URL string that
  // then lives in IndexedDB; the 320px JPEG below is ~30 KB and identical in
  // the 150px hover preview. Anything createImageBitmap cannot handle (SVG
  // without intrinsic size, exotic codecs) falls back to the data URL.
  if (file.type.startsWith("image/")) {
    try {
      return await generateImageThumbnail(file, IMAGE_THUMBNAIL_MAX_SIZE);
    } catch {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => resolve(reader.result as string);
        reader.onerror = () => reject(reader.error);
        reader.readAsDataURL(file);
      });
    }
  }

  // Handle PDF files
  if (file.type.startsWith("application/pdf")) {
    const scale = calculateScaleFromFileSize(file.size);

    // Only read first 2MB for thumbnail generation to save memory
    const chunk = file.slice(0, Math.min(LINEARIZED_PREFIX_BYTES, file.size));
    const arrayBuffer = await chunk.arrayBuffer();

    try {
      return await generatePDFThumbnail(
        arrayBuffer,
        scale,
        getPdfAccess(file)?.password,
      );
    } catch {
      // PDFium needs the xref table at the end of the file, so the 2MB
      // chunk can fail to open for PDFs larger than that. Retry with the
      // full buffer before falling back to an empty thumbnail.
      try {
        const fullArrayBuffer = await getDocumentBytes(file);
        return await generatePDFThumbnail(
          fullArrayBuffer,
          scale,
          getPdfAccess(file)?.password,
        );
      } catch (error) {
        reportThumbnailFailure(file, error);
        return "";
      }
    }
  }

  // Non-PDF, non-image files use scalable SVG icons in the UI — no raster thumbnail needed
  return "";
}

/**
 * Generate thumbnail and extract page count for a PDF file - always returns a valid thumbnail
 * @param applyRotation - If true, render thumbnail with PDF rotation applied (for static display).
 *                        If false, render without rotation (for CSS-based rotation in PageEditor)
 */
export async function generateThumbnailWithMetadata(
  file: File,
  applyRotation: boolean = true,
): Promise<ThumbnailWithMetadata> {
  // Non-PDF files have no page count
  if (!file.type.startsWith("application/pdf")) {
    const thumbnail = await generateThumbnailForFile(file);
    return { thumbnail, pageCount: 0 };
  }

  const scale = calculateScaleFromFileSize(file.size);

  // Never full-parse huge PDFs client-side - the renderer process OOMs long
  // before system RAM runs out. The prefix succeeds for linearized PDFs.
  if (file.size >= LARGE_PDF_PARSE_LIMIT) {
    // Probe inside the try: an unreadable file must still resolve, or the
    // caller leaves the card with no metadata and a spinner that never stops.
    try {
      if (!getPdfAccess(file) && (await looksEncryptedFromTrailer(file))) {
        return { thumbnail: "", pageCount: 1, isEncrypted: true };
      }
      const chunk = await file.slice(0, LINEARIZED_PREFIX_BYTES).arrayBuffer();
      const result = await renderPdfThumbnailPdfium(
        chunk,
        scale,
        applyRotation,
        false,
        getPdfAccess(file)?.password,
      );
      if (result.isEncrypted) {
        return { thumbnail: "", pageCount: 1, isEncrypted: true };
      }
      return {
        thumbnail: result.thumbnail,
        pageCount: result.pageCount,
        pageRotations: result.pageRotations,
        pageDimensions: result.pageDimensions,
      };
    } catch (error) {
      reportThumbnailFailure(file, error);
      return { thumbnail: "", pageCount: 0 };
    }
  }

  try {
    const arrayBuffer = await getDocumentBytes(file);
    // Always read per-page rotation: PageEditor renders thumbnails upright and
    // uses this as the rotation baseline, so skipping it corrupts saves.
    const result = await renderPdfThumbnailPdfium(
      arrayBuffer,
      scale,
      applyRotation,
      true,
      getPdfAccess(file)?.password,
    );

    if (result.isEncrypted) {
      return {
        thumbnail: "",
        pageCount: 1,
        isEncrypted: true,
      };
    }

    return {
      thumbnail: result.thumbnail,
      pageCount: result.pageCount,
      pageRotations: result.pageRotations,
      pageDimensions: result.pageDimensions,
    };
  } catch (error) {
    reportThumbnailFailure(file, error);
    return { thumbnail: "", pageCount: 1 };
  }
}

/**
 * Both thumbnail variants + page metadata from ONE full parse instead of two.
 * Large PDFs only get the linearized-prefix attempt; if that fails, both
 * variants are empty placeholders and page metadata is omitted.
 */
export async function generateThumbnailPairWithMetadata(file: File): Promise<{
  unrotated: ThumbnailWithMetadata;
  rotated: ThumbnailWithMetadata;
}> {
  const scale = calculateScaleFromFileSize(file.size);
  const isLarge = file.size >= LARGE_PDF_PARSE_LIMIT;
  let parsedBytes = 0;
  try {
    // Probe inside the try: an unreadable file must still resolve, or the
    // caller leaves the card with no metadata and a spinner that never stops.
    if (
      isLarge &&
      !getPdfAccess(file) &&
      (await looksEncryptedFromTrailer(file))
    ) {
      const encrypted: ThumbnailWithMetadata = {
        thumbnail: "",
        pageCount: 1,
        isEncrypted: true,
      };
      return { unrotated: encrypted, rotated: { ...encrypted } };
    }
    const buffer = isLarge
      ? await file.slice(0, LINEARIZED_PREFIX_BYTES).arrayBuffer()
      : await getDocumentBytes(file);
    parsedBytes = buffer.byteLength;
    const pair = await renderPdfThumbnailPairPdfium(
      buffer,
      scale,
      !isLarge,
      getPdfAccess(file)?.password,
    );

    const toPublic = (r: PdfiumRenderResult): ThumbnailWithMetadata =>
      r.isEncrypted
        ? { thumbnail: "", pageCount: 1, isEncrypted: true }
        : {
            thumbnail: r.thumbnail,
            pageCount: r.pageCount,
            pageRotations: r.pageRotations,
            pageDimensions: r.pageDimensions,
          };
    return {
      unrotated: toPublic(pair.unrotated),
      rotated: toPublic(pair.rotated),
    };
  } catch (error) {
    reportThumbnailFailure(file, error);
    // Damage needs PDFium to have rejected the whole file: a prefix (large files) or a short read
    // fails on bytes that are not the file. Unsupported encryption can also read as a format error.
    const isDamaged =
      !isLarge &&
      parsedBytes === file.size &&
      isDamagedPdfError(error) &&
      !(await looksEncryptedFromTrailer(file).catch(() => true));
    return {
      unrotated: { thumbnail: "", pageCount: 0, isDamaged },
      rotated: { thumbnail: "", pageCount: 0, isDamaged },
    };
  }
}