Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test a PDF download as an HTTP response, not just as a call to PDFKit. In an RSpec Rails request spec, verify the status, PDF content type, attachment disposition, filename, and response body. Stub PDFKit for fast, repeatable request coverage, then run a separate integration test with the real wkhtmltopdf executable to catch renderer and asset-configuration problems.

What a PDF download test should prove

A successful PDF endpoint has two parts: it generates PDF bytes and delivers them according to the route’s public contract. A response can return status 200 while containing an HTML error page, or contain PDF bytes with headers that make a browser display or handle the file incorrectly.

For a downloadable report, check these behaviors:

  • Status: usually 200 OK for a completed request.
  • Content type: application/pdf.
  • Disposition: includes attachment when the endpoint is intended to prompt a download.
  • Filename: the expected filename is present in Content-Disposition.
  • Body: contains PDF data rather than an HTML page or empty response. Starting with %PDF- is a useful minimum signature check.

These assertions test what a client receives. They are more useful than asserting only that a controller called a particular PDFKit method, because the response headers and body are the download contract.

Write a fast RSpec Rails request spec

Request specs exercise the route and Rails response stack. RSpec Rails maps request specs to Rails integration tests and provides Rails-aware matchers such as have_http_status. Its README recommends functional request tests over direct controller testing. See the RSpec Rails documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

The example below assumes a report route named report_path, a persisted report fixture or factory, and an endpoint that returns an attachment named report.pdf. Adjust those application-specific details to match your route and promised filename.

# spec/requests/reports_spec.rb
RSpec.describe "Reports", type: :request do
  describe "GET /reports/:id.pdf" do
    let(:pdf_bytes) { "%PDF-1.4nfixture pdf bytesn%%EOFn" }

    before do
      allow(PDFKit).to receive(:new).and_return(
        instance_double(PDFKit, to_pdf: pdf_bytes)
      )
    end

    it "returns a downloadable PDF" do
      get report_path(report, format: :pdf)

      expect(response).to have_http_status(:ok)
      expect(response.headers["Content-Type"]).to include("application/pdf")
      expect(response.headers["Content-Disposition"]).to match(/attachment/i)
      expect(response.headers["Content-Disposition"]).to include("report.pdf")
      expect(response.body).to start_with("%PDF-")
      expect(response.body).to include("%%EOF")
    end
  end
end

The PDF string is a small test fixture, not a rendered or standards-complete PDF. The signature and end-marker checks are practical sanity checks; they do not validate PDF structure or visual correctness. Keep assertions tied to the endpoint’s public behavior. If your application promises a different filename, disposition, or status, assert that contract instead.

Use a PDFKit double if the application constructs PDFKit with arguments or calls a different method; preserve the real request and response path while replacing only the external rendering work. For example, if the action calls PDFKit.new(html, options).to_pdf, the stub should tolerate or expect the options your implementation relies on rather than accidentally hiding a regression in those options.

Choose the right delivery path: send_data or send_file

Rails supports both returning generated bytes and sending a file already on disk. Test the delivery mechanism your action actually uses; do not make the request spec depend on a file that production does not create, or vice versa. The Rails Action Controller guide documents send_data and send_file, and distinguishes attachment behavior from inline display.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

When the action uses send_data

send_data is appropriate when the PDF is generated as bytes in the request. Exercise the endpoint and assert the response’s content type, disposition, filename, and body just as in the request-spec example. Rails documents send_data as a way to send generated data to the client.

When the action uses send_file

send_file serves a file already on disk. In the test, create a temporary or fixture PDF, have the action serve that file, and verify that the response contains the expected data and headers. This checks the actual file-delivery path rather than only checking that a file exists. Rails documents send_file for sending a file to the client.

Both methods can deliver an attachment. If the response is intended to display a PDF in the browser instead, test the inline disposition your endpoint promises rather than asserting attachment.

Keep real-renderer coverage separate

A stubbed request spec gives fast feedback on routing, authorization and application logic, response construction, headers, filename, and the bytes handed to Rails. It does not prove that wkhtmltopdf is installed, that PDFKit can launch it, or that the rendered template can load its CSS, images, and JavaScript.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Add a focused integration example or CI job that does not stub PDFKit. Render a representative template with the configured executable and assert the response contract plus a non-empty body. Keep this path separate from the fast request spec: PDFKit invokes an external process, and executable availability or asset URLs can differ between development and CI. See the PDFKit documentation for its wkhtmltopdf integration and troubleshooting guidance.

A useful division of coverage is:

  • Fast request spec on routine changes: stub the renderer and cover response behavior deterministically.
  • Real-renderer integration coverage: run with the actual executable to catch setup, rendering, and asset-loading failures.

Do not treat a passing signature check in the stubbed test as proof that the rendered document looks right. If visual output matters, the real-renderer path must exercise a representative template and its resource dependencies.

Configure wkhtmltopdf and assets explicitly

If the test environment cannot find the executable, configure PDFKit with its path. The path must point to an executable available in that environment; a developer-machine path will not automatically exist in CI.

PDFKit.configure do |config|
  config.wkhtmltopdf = "/path/to/wkhtmltopdf"
end

Relative asset references can also fail when the renderer does not resolve them in the application’s request context. PDFKit documents root_url and protocol options for resolving relative resources. Use absolute asset URLs or configure those options for the renderer environment, then verify the result in the real-renderer test. Missing stylesheets, images, or JavaScript can make a PDF render differently or incompletely even when the HTTP response itself succeeds.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

PDFKit also documents a possible deadlock when wkhtmltopdf calls back into a local application server configured with a single thread. For an integration environment that loads local assets, use multiple server workers or embed resources so the renderer does not need to make callbacks to a blocked server.

Diagnose common failures

Symptom Likely cause What to check or change
Browser displays text or mishandles the response The response has the wrong content type. Inspect Content-Type and return application/pdf. PDFKit specifically calls for that content type for a browser response.
Download does not start The response is inline, or its attachment filename is missing or unexpected. Inspect Content-Disposition; check for attachment and the promised filename. Rails distinguishes attachment from inline behavior.
Body begins with HTML instead of %PDF- The renderer may have failed, or the endpoint returned an error page. Use the signature assertion in the fast spec, then inspect the body and renderer output in the real-renderer integration path.
CSS or images are missing from the PDF Relative resources are not resolving in the renderer context. Use absolute URLs or configure PDFKit’s root_url and protocol options; confirm resource access in the integration environment.
PDFKit cannot start wkhtmltopdf The executable is absent from the environment or not discoverable at the expected path. Install the executable for that environment and set config.wkhtmltopdf to its actual path.
Rendering hangs while loading local assets The local server may be single-threaded while the renderer calls back into it. Use multiple workers in the integration environment or embed the resources to avoid callbacks.

When a failure appears only in CI, first compare the executable path and asset URL configuration between CI and local runs. Keep diagnostics in the real-renderer job; the stubbed request spec should remain independent of machine-specific binaries.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the test dependency version before copying setup

RSpec Rails compatibility depends on the Rails version. The current RSpec Rails README’s guidance lists 8.x for Rails 8.0 and 7.2, 7.x for Rails 7.x, 6.x for Rails 6.1, 7.0, and 7.1, and 5.x for Rails 5.2 and 6.x. Confirm the matching branch before copying a dependency declaration into an existing application: RSpec Rails version guidance.

Or skip the browser setup

If the task is capturing a website as a PDF rather than testing your own Rails endpoint, ScreenshotNeo is a screenshot API and MCP server for developers. Its PDF endpoint accepts a URL in one request; its response headers report the page verdict and whether the capture was billed. This is a different job from testing a PDF download endpoint in your Rails application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

For PDF output, use the API’s PDF options documented in the ScreenshotNeo API documentation. ScreenshotNeo accepts cookie banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, with the response indicating the page verdict and billing status. An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.

The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo’s free plan to start capturing without a card.

Frequently Asked Questions

Does a PDF signature check prove the file is valid?

No. Checking for %PDF- catches obvious non-PDF responses but does not validate the document’s full structure or visual appearance.

Should the real-renderer integration test run for every request-spec example?

No. Keep routine request coverage fast with PDFKit stubbed, and run focused real-renderer coverage where the executable and asset integration are available.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.