or run

npx @tessl/cli init
Log in

Version

Files

docs

browser-contexts.mdbrowser-management.mdelement-handling.mdindex.mdinput-simulation.mdlocators-selectors.mdnetwork-management.mdpage-interaction.md
tile.json

task.mdevals/scenario-8/

Web Page to PDF Converter

Overview

Create a command-line tool that converts web pages to PDF documents with custom formatting options. The tool should accept a URL and produce a PDF file with specified page size, orientation, and margin settings.

Requirements

Core Functionality

  1. URL to PDF Conversion

    • Accept a URL as input
    • Load the web page completely (wait for network to be idle)
    • Generate a PDF file from the loaded page
  2. Customizable PDF Options

    • Support multiple page sizes: A4, Letter, and Legal
    • Support both portrait and landscape orientations
    • Allow custom margins (top, right, bottom, left)
    • Include printed backgrounds and graphics in the output
  3. Command-Line Interface

    • Accept URL as the first argument
    • Accept output file path as the second argument
    • Accept optional configuration flags:
      • --size (default: A4)
      • --landscape (flag for landscape orientation)
      • --margin (format: "top,right,bottom,left" in inches)
    • Display error messages for invalid inputs

Implementation Details

  • The tool should run in headless mode for automation
  • The PDF should include all page backgrounds and graphics by default
  • Handle navigation errors gracefully
  • Close the browser instance after PDF generation

Dependencies { .dependencies }

puppeteer-core { .dependency }

Provides browser automation and PDF generation capabilities.

Test Cases

Test Case 1: Basic PDF Generation @test

File: converter.test.ts

Test: Generate a basic PDF from a simple HTML page in A4 format with default settings

import { generatePDF } from './converter';
import * as fs from 'fs';

test('generates PDF with default A4 format', async () => {
  const outputPath = './test-output-a4.pdf';
  await generatePDF('https://example.com', outputPath, {});

  expect(fs.existsSync(outputPath)).toBe(true);
  const stats = fs.statSync(outputPath);
  expect(stats.size).toBeGreaterThan(0);

  // Cleanup
  fs.unlinkSync(outputPath);
});

Test Case 2: Custom Format and Orientation @test

File: converter.test.ts

Test: Generate a PDF in Letter format with landscape orientation

test('generates PDF in landscape Letter format', async () => {
  const outputPath = './test-output-letter.pdf';
  await generatePDF('https://example.com', outputPath, {
    format: 'Letter',
    landscape: true
  });

  expect(fs.existsSync(outputPath)).toBe(true);

  // Cleanup
  fs.unlinkSync(outputPath);
});

Test Case 3: Custom Margins @test

File: converter.test.ts

Test: Generate a PDF with custom margins specified

test('generates PDF with custom margins', async () => {
  const outputPath = './test-output-margins.pdf';
  await generatePDF('https://example.com', outputPath, {
    format: 'A4',
    margin: {
      top: '0.5in',
      right: '0.5in',
      bottom: '0.5in',
      left: '0.5in'
    }
  });

  expect(fs.existsSync(outputPath)).toBe(true);

  // Cleanup
  fs.unlinkSync(outputPath);
});

Expected Behavior

  • The tool should successfully navigate to the provided URL
  • The PDF should be saved to the specified output path
  • Invalid URLs should result in appropriate error messages
  • The browser should be properly cleaned up after PDF generation
  • Generated PDFs should preserve the visual appearance of the web page