PDFtoImage

repository·master·Indexed 18 days ago

https://github.com/sungaila/pdftoimage

A .NET library (version 5.3.0) used to render PDF files into images such as PNG, JPEG, WebP, or SkiaSharp bitmaps. It leverages PDFium for rendering and SkiaSharp for cross-platform graphics support. The library supports various runtimes including .NET, .NET Framework, and Mono, and is compatible with frameworks like ASP.NET Core, Blazor WebAssembly, .NET MAUI (excluding iOS), Unity (excluding iOS), and UWP.

Tokens
2.1K
Snippets
10
Records
16
Agent score
63%

What's inside PDFtoImage

  1. Concurrency and Parallelization limitations

    master

    The underlying native PDFium library is not thread-safe. Consequently, all calls into PDFium are protected with locks within this library.

    Key constraints:

    • You can only process one PDF at a time within a single process.
    • True parallel processing cannot be achieved via multi-threading in a single process.

    Workaround for parallel processing: If you require parallel execution, you must spawn multiple separate processes and distribute the workload across them, using inter-process communication (IPC) and serialization to collect results.

  2. Run AotConsole container smoke tests on Linux

    master

    You can build and run smoke tests for the AotConsole using Docker BuildKit. The build contexts are located at the repository root. The Linux Dockerfiles support both linux/amd64 and linux/arm64 architectures via the TARGETARCH argument.

    Note on Performance: Native AOT builds compile on TARGETPLATFORM. It is highly recommended to use native ARM64 runners for linux/arm64 builds, as AOT compilation under QEMU is significantly slower.

    # Build and run for linux/amd64
    docker buildx build --load --platform linux/amd64 \
      -f src/FrameworkTests/AotConsole/Dockerfiles/ubuntu.dockerfile \
      -t pdftoimage-smoke:ubuntu .
    docker run --rm pdftoimage-smoke:ubuntu
    
    # Build and run for linux/arm64
    docker buildx build --load --platform linux/arm64 \
      -f src/FrameworkTests/AotConsole/Dockerfiles/alpine-aot.dockerfile \
      -t pdftoimage-smoke:alpine-aot .
    docker run --rm --platform linux/arm64 pdftoimage-smoke:alpine-aot
  3. Run AotConsole container smoke tests on Windows

    master

    Windows smoke tests are performed using Windows SDK containers.

    • Build Types: Framework-dependent, self-contained, and single-file builds are produced within these containers.
    • Native AOT: Because Windows Native AOT requires the MSVC toolchain on the host, the AOT Dockerfiles do not perform the compilation; they only package a previously published win-x64 directory.
    • Nano Server: Nano Server variants are used as extended compatibility tests to identify native Win32 imports that might be unavailable in the Nano Server environment.
  4. Get started with PDFtoImage

    master

    PDFtoImage is a .NET library that renders PDF files into images using PDFium and SkiaSharp.

    To render a single page to a specific format, use the static methods in PDFtoImage.Conversion. Available methods include:

    • SavePng
    • SaveJpeg
    • SaveWebp
    • ToImage (returns a SkiaSharp.SKBitmap)

    To render multiple pages, use:

    • ToImages
    • ToImagesAsync
    using var pdf = File.OpenRead("document.pdf");
    
    PDFtoImage.Conversion.SavePng(
        imageFilename: "page1.png",
        pdfStream: pdf,
        page: 0);
  5. Use Open Iconic's SVG Sprite

    master

    The SVG sprite allows you to load all icons in a single request. To use an icon, place a <use> tag inside an <svg> element and reference the icon ID from the sprite file using xlink:href.

    Styling

    • Sizing: Set equal width and height on the <svg> tag.
    • Coloring: Set the fill property on the <use> tag.

    Tip: Add a general class to the <svg> tag for shared styles and a unique class to the <use> tag for icon-specific styles (like color).

    <svg class="icon">
      <use xlink:href="open-iconic.svg#account-login" class="icon-account-login"></use>
    </svg>
  6. Install PDFtoImage in Unity

    master

    To install the PDFtoImage package in a Unity project, follow these steps:

    1. Open your project and navigate to WindowPackage ManagementPackage Manager.
    2. Click the + button in the top-left corner.
    3. Select Install package from git URL....
    4. Enter the following URL and click Install: https://github.com/sungaila/PDFtoImage.git?path=etc/UnityPackage
    https://github.com/sungaila/PDFtoImage.git?path=etc/UnityPackage
  7. Convert a single PDF page to an image

    master

    To render a single page of a PDF into an image format, use the static methods provided by PDFtoImage.Conversion. Supported formats include JPEG, PNG, and WebP.

    Available methods for single pages:

    • SaveJpeg
    • SavePng
    • SaveWebp
    • ToImage (returns a SkiaSharp.SKBitmap)

    Note: If you use ToImage, you can export the resulting SkiaSharp.SKBitmap using the SkiaSharp Encode method.

    using var pdf = File.OpenRead("document.pdf");
    
    PDFtoImage.Conversion.SavePng(
        imageFilename: "page1.png",
        pdfStream: pdf,
        page: 0);