# KDP Book Generator

[![CI](https://github.com/zoyth/kdp-book-generator/workflows/CI/badge.svg)](https://github.com/zoyth/kdp-book-generator/actions)
[![npm version](https://badge.fury.io/js/kdp-book-generator.svg)](https://www.npmjs.com/package/kdp-book-generator)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Node.js Version](https://img.shields.io/node/v/kdp-book-generator.svg)](https://nodejs.org)

A powerful tool to generate KDP-compliant PDFs from Markdown files for Amazon book publishing.

## Features

- ✅ **KDP Compliance**: Automatic margin calculation, font embedding, and format validation
- 📐 **Multiple Formats**: Support for all standard KDP book sizes (6"x9", 5.5"x8.5", etc.)
- 🎨 **Precise Typography**: Full control over fonts, spacing, and layout
- 📄 **Professional Layout**: Page numbers, table of contents, chapter breaks
- 🖨️ **Print Ready**: 300 DPI support with optional bleed
- ⚡ **Fast Generation**: Playwright-based PDF generation for consistent results
- 📱 **EPUB/Kindle Support**: Generate e-books for Kindle Direct Publishing
- 📚 **Multi-file Assembly**: Combine multiple Markdown files into complete books

## Installation

```bash
npm install -g kdp-book-generator
```

Or use directly with npx:

```bash
npx kdp-book-generator generate my-book.md
```

## Quick Start

1. Create a markdown file with your book content:

```markdown
---
title: "My Amazing Book"
author: "Your Name"
---

# Chapter 1: Introduction

Welcome to my book! This is the first chapter...

---

# Chapter 2: Getting Started

Here's how to get started...
```

2. Generate your PDF:

```bash
kdp-generator generate my-book.md --output my-book.pdf --format 6x9
```

## CLI Usage

### Generate PDF

```bash
kdp-generator generate <input.md> [options]
```

### Generate EPUB (for Kindle)

```bash
kdp-generator generate <input.md> --output book.epub --format kindle
```

**Options:**
- `-o, --output <output>` - Output PDF file (default: book.pdf)
- `-f, --format <format>` - Book format: 6x9, 5.5x8.5, 5x8, 8.5x11, 7x10, 7.5x9.25 (default: 6x9)
- `-c, --config <config>` - Configuration file (JSON)
- `-t, --title <title>` - Book title
- `-a, --author <author>` - Book author
- `--no-toc` - Disable table of contents
- `--bleed` - Enable bleed (0.125" on all sides)
- `--pages <pages>` - Estimated page count for margin calculation (default: 200)
- `--debug` - Enable debug output

### List Available Formats

```bash
kdp-generator formats
```

### Generate Configuration File

```bash
kdp-generator config --output book-config.json
```

### Validate Configuration

```bash
kdp-generator validate book-config.json
```

### Assemble Multiple Files

```bash
kdp-generator assemble assembly-config.yaml --output complete-book.pdf

# For EPUB/Kindle
kdp-generator assemble assembly-config.yaml --output book.epub --format kindle
```

## Configuration

Create a `book-config.json` file to customize your book:

```json
{
  "title": "My Book",
  "author": "Author Name",
  "format": {
    "name": "6\" x 9\"",
    "width": 6,
    "height": 9,
    "unit": "in"
  },
  "margins": {
    "top": 0.25,
    "bottom": 0.25,
    "inside": 0.5,
    "outside": 0.25,
    "unit": "in"
  },
  "typography": {
    "body": {
      "family": "Times New Roman",
      "size": 11,
      "lineHeight": 1.4,
      "weight": "normal",
      "style": "normal",
      "color": "#000000",
      "marginTop": 0,
      "marginBottom": 12
    },
    "heading1": {
      "family": "Times New Roman",
      "size": 18,
      "lineHeight": 1.4,
      "weight": "bold",
      "style": "normal",
      "color": "#000000",
      "marginTop": 24,
      "marginBottom": 18
    }
  },
  "pageNumbers": {
    "enabled": true,
    "startPage": 1,
    "format": "decimal",
    "position": "footer",
    "alignment": "center"
  },
  "tableOfContents": true,
  "bleed": false
}
```

## Markdown Features

### Front Matter

Use YAML front matter to specify book metadata:

```markdown
---
title: "Book Title"
author: "Author Name"
description: "Book description"
keywords: ["keyword1", "keyword2"]
language: "en"
---
```

### Page Breaks

Use horizontal rules to force page breaks:

```markdown
# Chapter 1

Content here...

---

# Chapter 2

New chapter on new page...
```

### Table of Contents

Automatically generated from headings. Use `--no-toc` to disable.

### Supported Elements

- **Headings**: H1-H6 with automatic styling
- **Paragraphs**: Justified text with proper spacing
- **Lists**: Ordered and unordered lists
- **Blockquotes**: Styled quote blocks
- **Code**: Inline code and code blocks
- **Tables**: Formatted tables with borders
- **Images**: Embedded images (300 DPI recommended)
- **Links**: Clickable links in PDF

## Amazon KDP Compliance

### Automatic Margin Calculation

Margins are automatically calculated based on your page count:

- **24 pages or less**: 0.375" inside, 0.25" outside
- **25-150 pages**: 0.5" inside, 0.25" outside
- **151-300 pages**: 0.625" inside, 0.25" outside
- **301-500 pages**: 0.75" inside, 0.25" outside
- **501+ pages**: 0.875" inside, 0.25" outside

### Font Embedding

All fonts are automatically embedded in the PDF to ensure compatibility.

### Quality Requirements

- **Resolution**: 300 DPI for all images
- **File size**: Optimized to stay under 650MB limit
- **Color mode**: Proper color handling for print
- **Format compliance**: Meets all KDP technical requirements

## Programmatic Usage

```javascript
import { generatePDF } from 'kdp-book-generator';

await generatePDF({
  input: 'my-book.md',
  output: 'my-book.pdf',
  config: {
    title: 'My Book',
    author: 'Author Name',
    format: { name: '6" x 9"', width: 6, height: 9, unit: 'in' },
    // ... other config options
  },
  debug: true
});
```

## Examples

See the `examples/` directory for:
- `sample-book.md` - Complete book example
- `assembly-config.yaml` - Multi-file book assembly
- `simple-config.json` - Basic configuration

See the `templates/` directory for:
- `book-assembly-template.yaml` - Comprehensive assembly template
- `simple-book-assembly.yaml` - Minimal assembly example
- `textbook-assembly.yaml` - Academic book structure
- `novel-assembly.yaml` - Fiction book structure

## Documentation

- [Multi-File Assembly Guide](docs/MULTI-FILE-ASSEMBLY.md) - Combine multiple files into books
- [EPUB/Kindle Guide](docs/EPUB-KINDLE-GUIDE.md) - Generate e-books for Amazon KDP

## Requirements

- Node.js 18 or higher
- Chromium (automatically installed with Playwright)

## Development

```bash
# Install dependencies
npm install

# Build the project
npm run build

# Run in development mode
npm run dev

# Run tests
npm test
```

## License

MIT License - see LICENSE file for details.

## Contributing

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## Support

For issues and questions:
- Create an issue on GitHub
- Check the documentation
- Review the examples

## Changelog

### 1.0.0
- Initial release
- KDP compliance features
- Multiple format support
- CLI interface
- Configuration system