Skip to content

fix(email-service): invoice attachment example produces a damaged PDF #35753

Done

Description

@coygeek

Existing documentation URL(s)

What changes are you suggesting?

The example's successful invoice path should produce a readable PDF invoice as the bytes of its application/pdf attachment.

Additional information

Summary

The documented invoice Worker returns success and passes a PDF attachment to its email binding. PDF validation reports structural damage in the generated file.

Runtime or environment

Published Cloudflare developer documentation and its production source baseline. Local probe used Node v26.13.1 and qpdf 12.4.2.

Steps to reproduce

  1. Copy the TypeScript blocks from the Email attachments tutorial into one probe.mts file in page order. Name the handler const worker = {...} so the local probe can call it.
  2. Append the fixture below. Run it with a recent Node that can strip TypeScript syntax and with qpdf available. The fake binding writes the attachment bytes to a file and makes no network calls.
  3. Run TZ=UTC LANG=C node probe.mts and then qpdf --check invoice-42.pdf.
Reproduction script: probe.mts fixture to append
import { writeFile } from "node:fs/promises";

const request = new Request("https://example.test/send-invoice", {
  method: "POST",
  body: JSON.stringify({
    customerEmail: "recipient@example.test",
    invoiceData: {
      number: "42", customerName: "Example Customer", date: 0, dueDate: 0, total: 5,
    },
  }),
});
const response = await worker.fetch(request, {
  DOMAIN: "example.test",
  EMAIL: {
    async send({ attachments }) {
      const attachment = attachments[0];
      await writeFile(attachment.filename, Buffer.from(attachment.content));
      return { messageId: "fixture" };
    },
  },
});
console.log(response.status, await response.text());

Actual behavior

The local handler returned HTTP 200 with Invoice sent. qpdf exited 3 and reported file is damaged, (offset 400): xref not found, and recovered stream length: 148. The example declares a fixed stream length of 200 and a fixed cross-reference location even though invoice text changes. The check exercised attachment generation and did not send an email.

Evidence

Restoration check

Run the documented invoice path with the fixture and a second invoice with a longer customer name. The captured file for each invoice opens as a PDF, contains its invoice data, and qpdf --check returns exit 0 without repair warnings.

Triage assessment

  • Impact level: P2
  • Assessment status: supported
  • Impact basis: Copying the worked example produces a damaged invoice attachment and the handler reports success.
  • Workaround status: available
  • Workaround basis: Use valid PDF bytes from a PDF tool or a separately generated invoice with the same email attachment API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions