Skip to content

Codebucket-Solutions/puppet-master-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@codebucket/puppet-master

TypeScript client for the Puppet Master HTML-to-PDF server.

Puppet Master server: Codebucket-Solutions/puppet-master

The uploaded server code shows that this client talks to a single POST /pdf endpoint, sends a JSON body shaped like { task: ... }, and downloads the generated PDF as a file stream.

Install

npm install @codebucket/puppet-master

Quick Start

import { PuppetMaster } from "@codebucket/puppet-master";

const client = new PuppetMaster({
  baseUrl: process.env.PUPPET_MASTER_BASE_URL!,
  apiKey: process.env.PUPPET_MASTER_API_KEY!,
});

await client.pdf({
  pdfPath: "./invoice.pdf",
  content: `
    <!doctype html>
    <html>
      <body>
        <h1>Invoice</h1>
        <p>Hello from Puppet Master.</p>
      </body>
    </html>
  `,
  pageOptions: {
    waitUntil: "networkidle0",
  },
  pdfOptions: {
    format: "A4",
    printBackground: true,
  },
});

CommonJS Usage

const { PuppetMaster } = require("@codebucket/puppet-master");

const puppetMaster = new PuppetMaster({
  baseUrl: process.env.PUPPETMASTER_BASEURL,
  apiKey: process.env.PUPPETMASTER_APIKEY,
});

await puppetMaster.pdf({
  content: allPages,
  launchOptions: {
    args: ["--no-sandbox"],
  },
  pageOptions: { waitUntil: "networkidle0" },
  pdfOptions: {
    format: "A4",
    printBackground: true,
  },
  pdfPath: "path to your pdf",
});

Public API

import {
  PuppetMaster,
  type PuppetMasterClientOptions,
  type PdfTaskOptions,
  type RemotePdfTaskOptions,
  type PageFunctionCall,
} from "@codebucket/puppet-master";

new PuppetMaster(options)

interface PuppetMasterClientOptions {
  baseUrl: string;
  apiKey: string;
}

await client.pdf(task)

type PageFunctionCall =
  | [methodName: string]
  | [methodName: string, args: unknown[]];

interface RemotePdfTaskOptions {
  launchOptions?: Record<string, unknown>;
  pageOptions?: Record<string, unknown>;
  pdfOptions?: Record<string, unknown>;
  otherPageFunctions?: PageFunctionCall[];
  content: string;
}

interface PdfTaskOptions extends RemotePdfTaskOptions {
  pdfPath: string;
}

What The Server Actually Does

Based on the uploaded server code:

  1. POST /pdf receives { task }.
  2. The worker launches Puppeteer with task.launchOptions.
  3. It creates a page and calls page.setContent(task.content, task.pageOptions).
  4. It generates a PDF with page.pdf({ ...task.pdfOptions, path }).
  5. It optionally runs task.otherPageFunctions.
  6. The server downloads the generated file back to the client.

Important caveat:

  • In the current server implementation, otherPageFunctions runs after page.pdf(). That means those calls do not affect the PDF that was already generated.

Wire Format

The client sends this request body to the server:

{
  "task": {
    "content": "<html>...</html>",
    "launchOptions": {},
    "pageOptions": {},
    "pdfOptions": {},
    "otherPageFunctions": [
      ["waitForNetworkIdle", [{ "idleTime": 500 }]]
    ]
  }
}

Notes:

  • pdfPath is local-only. It is where this client writes the downloaded PDF on the caller's machine.
  • The client always sends the x-api-key header.
  • The uploaded server route currently has API-key validation commented out, but the contract still reserves that header.

otherPageFunctions Format

Use an ordered array of page method calls, not an object.

Examples:

const waitForIdle: PageFunctionCall = ["waitForNetworkIdle", [{ idleTime: 500 }]];
const emulateScreen: PageFunctionCall = ["emulateMediaType", ["screen"]];
const bringToFront: PageFunctionCall = ["bringToFront"];

Agent Guidance

If you are generating code with AI:

  1. Install @codebucket/puppet-master.
  2. Import from the package root.
  3. Create one PuppetMaster client with baseUrl and apiKey.
  4. Call only await client.pdf(...) because this package currently exposes only one public method.
  5. Provide content and pdfPath every time.
  6. Model otherPageFunctions as tuple arrays, not as a map or object.
  7. Do not send pdfPath in hand-written HTTP requests to the server; that field is client-side only.

Minimal Agent Example

import { PuppetMaster, type PageFunctionCall } from "@codebucket/puppet-master";

async function renderPdf(html: string, outputPath: string) {
  const otherPageFunctions: PageFunctionCall[] = [
    ["waitForNetworkIdle", [{ idleTime: 500 }]],
  ];

  const client = new PuppetMaster({
    baseUrl: process.env.PUPPET_MASTER_BASE_URL!,
    apiKey: process.env.PUPPET_MASTER_API_KEY!,
  });

  await client.pdf({
    content: html,
    pdfPath: outputPath,
    otherPageFunctions,
  });
}

Machine-Readable Schema

For tooling and agent integration, see schemas/pdf-task-options.schema.json.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors