How to Display PDFs from IPFS on the Web (Without Losing Your Mind Over Gateways)

 

If you have ever tried to integrate IPFS (InterPlanetary File System) into a traditional web application, you’ve likely stumbled upon the worst-kept secret of decentralization: public gateway latency.

The core concept is brilliant: you upload a PDF, get a unique and immutable CID (Content Identifier), and you’re good to go. The file is secure, distributed, and tamper-proof. But the moment you need to pass that file, for exampleto a browser-based PDF viewer, a major hurdle arises: Which public gateway will respond first? And what if that specific gateway is down today?

How to Render PDFs from IPFS on the Web
How to Render PDFs from IPFS on the Web

Today, we’ll explore how I solved this issue on the backend, optimizing performance using a parallel gateway racing system and smart caching.

The Problem: The IPFS Gateway Lottery

To display an IPFS file inside an iframe or a JavaScript viewer, we need a public HTTP gateway (such as Cloudflare, Pinata, or Web3.Storage) to translate the IPFS protocol into a standard https:// URL.

The catch is that public gateways are highly unpredictable:

  • A gateway might be lightning-fast right now and painfully slow five minutes later.
  • Some will time out if the file hasn’t fully propagated across the network yet.
  • Hardcoding a single gateway introduces a single point of failure (SPOF).

The solution? Pit the gateways against each other.
First one to respond wins.

Architecture of the Solution (In a Nutshell…)

To guarantee maximum loading speeds for the Document Viewer, we have implemented a three-step strategy:

Session Memory: It remembers the last gateway that responded successfully during the user’s active session.
Parallel Gateway Racing: If the preferred gateway fails or lags, it queries a list of alternative gateways simultaneously using asynchronous HTTP requests.
Smart Caching & Fallback: It caches the winning URL (e.g., via SQLite or JSON) to avoid re-running the race on every page refresh. If the entire IPFS network is unreachable, it seamlessly falls back to a locally stored file.

Implementation: The curl_multi_init Trick

Here is a conceptual Snippet showing how to implement parallel checks in PHP. Instead of testing gateways sequentially (which would take ages), we fire off HEAD requests all at once.
PHP


// A list of public gateways to put to the test
$default_gateways = [
    'https://gateway.pinata.cloud/ipfs',
    'https://cloudflare-ipfs.com/ipfs',
    'https://w3s.link/ipfs'
];

function checkIPFSGatewayParallel($cid, $gateways) {
    $mh = curl_multi_init();
    $chs = [];

    // Prepare asynchronous requests (HEAD only, to keep it lightning fast)
    foreach ($gateways as $gateway) {
        $url = rtrim($gateway, '/') . '/' . $cid;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, true); // Don't download the PDF, just look for a 200 OK
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        curl_multi_add_handle($mh, $ch);
        $chs[$gateway] = $ch;
    }

    // Execute handles in parallel
    $active = null;
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    $found_url = false;
    // ... descriptor read loop ...
    // The first gateway to return HTTP Code 200 breaks the loop and wins the race!
    
    return $found_url;
}

Why is this approach so efficient?

By setting CURLOPT_NOBODY = true, we issue a HEAD request. We aren’t downloading the actual PDF (which could be several megabytes); we are merely asking the gateway: “Hey, do you have this CID ready to serve?“. The first one to reply with a 200 OK wins the right to serve the file to our viewer.

Here the Global Logical Flow; within the application lifecycle, verifying the CID follows an optimized pipeline:

1 – Cache Check: Has this CID been linked to a working gateway in the last hour? If yes, use it.
2 – Fast Track: Try the gateway stored in the user’s session ($_SESSION[‘preferred_gateway’]). Estimated latency: just a few milliseconds.
3 – The Race: If the Fast Track fails, the parallel race described above triggers. The winner is saved to both the session and the cache.
4 – Hard Fallback: If the IPFS network is experiencing temporary congestion, the system switches to the local file system (uploads/file.pdf), ensuring the user always sees their document.

Once the definitive URL is fetched (whether from IPFS or local storage), you simply pass it to your client-side viewer (like an iframe or to a webpage instance), and you are good to go.

Want to Skip the Headache?

Dealing with cURL timeouts, configuring caching databases, managing local fallbacks, and tweaking viewers to avoid CORS issues with IPFS gateways can quickly turn into a micro-management nightmare.

If you want to integrate an immutable, decentralized, and blazing-fast Document Management System into your projects without writing, testing, and maintaining all this backend boilerplate… well, there’s Dokky Suite. It handles exactly this (and a whole lot more) natively, with just one click. 😉

Dokky Suite v2.3.0: Lighter, More Self-Contained, Ready for Growth

 

With version 2.3.0, Dokky Suite Script takes a significant step toward a clear objective: offering a Document Management and Collaboration Platform that is simpler to install, easier to manage, and more robust for daily use.

This release introduces significant improvements, both under the hood and in the user experience, with a precise focus on autonomy, portability, and overall quality. The result is a Suite that is more modern, more flexible, and, above all, better suited to real-world environments where reliability, speed, and deployment freedom are paramount.

Dokky Suite - where Documents meet Possibilities
Dokky Suite – where Documents meet Possibilities

One of the most significant changes concerns PDF preview generation.
We have eliminated the server-side dependency on ImageMagick, thereby simplifying the architecture and tangibly reducing installation and compatibility hurdles. This makes Dokky easier to deploy even in resource-constrained environments, without sacrificing essential functionality.

Another major step forward is our new approach to OCR (Optical Character Recognition).
The System has been redesigned as a native, self-hosted process with no mandatory external dependencies. In practical terms, Dokky can now handle text extraction autonomously while retaining the ability to integrate with third-party services whenever necessary. The advantage is clear: greater control for Administrators and greater flexibility for end Users.

On the User experience front, we have enhanced the Document Editing page and various user-specific options, with the aim of making daily workflows clearer and faster. Every detail has been carefully crafted to help those working with Documents Achieve more with fewer steps.

The search engine has also received a major update.
Both global search and tag-based search functions have been improved to deliver more precise and consistent results by more effectively cross-referencing available information. This means finding what truly matters, and finding it faster, even as your Document Archive grows larger and more complex. We have also updated the installer, making it clearer and more effective at checking system requirements.
This translates into a simpler onboarding process and a more reliable initial Setup (two fundamental aspects for anyone looking to get started without wasting time).

On the administrative side, the global Dashboard has been updated, along with the MySQL database, translations, and FAQs.
Rounding out the release is a series of minor bug fixes and performance enhancements that contribute to a smoother and more stable overall user experience.

Dokky Suite v2.3.0 is a release designed to combine power with simplicity.
Fewer dependencies, greater autonomy, improved search capabilities, and enhanced document control: everything converges to create a more mature and competitive Platform.

If you are looking for a self-hosted solution to manage, publish, and distribute documents using a modern, flexible approach, this is the perfect release to discover its value.

Discover Dokky Suite, try the Live Demo, and see how it can fit into your workflow.
If you want a Document Management Platform that combines autonomy, portability, and control, Dokky is ready for the next step!

Dokky Suite v2.2.0: Decentralization meets the BitTorrent protocol

 

From day one, the philosophy behind Dokky has been crystal clear: to offer a self-hosted, decentralized, clean, and lightweight collaboration platform, free from burdensome external dependencies. Today, with the official release of v2.2.0, we’re taking a step toward complete resilience and independence in your Document Distribution.

Welcome to the era of Dokky P2P.

Dokky Suite - v2.2
Dokky Suite – v2.2

Why the BitTorrent protocol in Dokky?

Anyone who runs a Document Distribution Platform knows that traffic and bandwidth can become a significant bottleneck (and cost). Traditionally, if a Document goes viral, the server has to handle every single download request, with the risk of overloading or increasing infrastructure costs.

With the new native P2P module, Dokky embraces true philanthropic Decentralization and solves this problem at its root through three engineering pillars:

Total Resilience: Documents no longer rely exclusively on the central server. Once they’re uploaded to the Torrent network, their availability becomes distributed.

Native Web Seeding: By leveraging the HTTP-Source architecture, the Dokky instance itself acts as the initial source. This eliminates the classic “missing seed” problem in Torrent networks: users’ downloads will always start instantly, even if they’re the first to request the file.

Integrated Tracker: Dokky doesn’t rely on external public trackers that could track data or go offline. It includes a well-coded native tracker written entirely in PHP that securely maps peers in real time, optimizing direct connections between platform users. Data retention is temporary, following GDPR guidelines.

If the administrator decides to activate the feature from the backend, the platform transforms into a highly efficient mass distribution ecosystem, perfect for businesses, independent publishers, or research communities.

Dokky P2P Features
Dokky P2P Features

This release represents the latest in a highly intensive development journey that has radically transformed our suite over the past few months. The penultimate major step was taken with v2.1, a monumental release focused on core editing: we introduced Dokky Publisher, the native and fully self-hosted PDF creation engine (without any external API), complete with 4 professional layouts, lossless document lifecycle management, ISO 19005-3 (PDF-A3B) compliance, and the ability to merge multiple versions into a single, independent PDF eBook.

Today, with v2.2, we’re closing the loop by combining all this publishing power with the resilience of the P2P network, simultaneously updating the native WebWall module to v2.1 and revamping the administration interface.

A Powerful Technology That Requires Responsibility

The Decentralized nature of the BitTorrent protocol brings with it a fundamental dynamic that every user and administrator must fully understand. So, let’s conclude with an important note on privacy and data management: Once a document is shared via the P2P system, it is released into a network of independent nodes. Even if the original document is subsequently deleted from the Dokky platform (resulting in the automatic removal of the associated .torrent file on the server), it is not technically possible to delete or recall the peers and copies of the file already present within the network of users who downloaded it.

This feature represents an extraordinary weapon of freedom for the dissemination of knowledge, but it must be used absolutely responsibly, in full compliance with copyright laws and document ownership rights.

Clean, Lightweight, Independent

Every single line of the code written for this update faithfully upholds the promise of our architectural manifesto: Clean PHP, transparent code, and zero superfluous dependencies. Dokky requires no massive external libraries to handle bencode encoding or file tracking; everything runs natively on your server, guaranteeing you absolute control over your data, your privacy, and your operational costs.

Dokky v2.2.0 is ready. Update your instances, activate the P2P module, and unleash the full potential of your documents!

Live Demo