|
| 1 | +# Custom Cover Page Feature |
| 2 | + |
| 3 | +## Overview |
| 4 | +The PDF build process now automatically replaces the first page (auto-generated title page) with a custom cover image. |
| 5 | + |
| 6 | +## Cover Image |
| 7 | +- **Location**: `chapters/media/TS_SysmonCommunityGuide_Cover.png` |
| 8 | +- **Format**: PNG |
| 9 | +- **Size**: ~3 MB (high resolution) |
| 10 | +- **Page Size**: Scaled to fit A4 with aspect ratio maintained |
| 11 | + |
| 12 | +## Implementation |
| 13 | + |
| 14 | +### Components |
| 15 | +1. **`Build/replace_cover.py`** - Python script that handles cover replacement |
| 16 | + - Uses `pypdf` (or PyPDF2) to manipulate PDF pages |
| 17 | + - Uses `Pillow` to process cover image |
| 18 | + - Uses `reportlab` to convert image to PDF page |
| 19 | + - Maintains A4 page size and centers image |
| 20 | + |
| 21 | +2. **`Build/md2pdf.sh`** - Updated to call cover replacement after PDF generation |
| 22 | + - Generates PDF with XeLaTeX (2 passes for TOC) |
| 23 | + - Checks for cover image existence |
| 24 | + - Replaces first page automatically |
| 25 | + - Handles errors gracefully (falls back to original if replacement fails) |
| 26 | + |
| 27 | +3. **Dependencies** - Added Python packages: |
| 28 | + - `pypdf` - PDF manipulation |
| 29 | + - `Pillow` - Image processing |
| 30 | + - `reportlab` - PDF generation |
| 31 | + |
| 32 | +### Build Process Flow |
| 33 | +``` |
| 34 | +1. Pandoc converts Markdown → LaTeX |
| 35 | +2. XeLaTeX generates PDF (2 passes) |
| 36 | +3. Custom cover replacement: |
| 37 | + a. Check if TS_SysmonCommunityGuide_Cover.png exists |
| 38 | + b. Convert cover image to PDF page (A4, centered, scaled) |
| 39 | + c. Replace first page of generated PDF with cover |
| 40 | + d. Save final PDF |
| 41 | +4. Clean up temporary files |
| 42 | +``` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### Normal Build |
| 47 | +```bash |
| 48 | +# Just build as usual - cover replacement happens automatically |
| 49 | +make pdf |
| 50 | + |
| 51 | +# Or using build.sh |
| 52 | +./build.sh pdf |
| 53 | +``` |
| 54 | + |
| 55 | +### Installing Dependencies |
| 56 | + |
| 57 | +**macOS:** |
| 58 | +```bash |
| 59 | +make install-deps-mac |
| 60 | +# Or manually: |
| 61 | +pip3 install --user --break-system-packages pypdf Pillow reportlab |
| 62 | +``` |
| 63 | + |
| 64 | +**Ubuntu/Debian:** |
| 65 | +```bash |
| 66 | +make install-deps |
| 67 | +# Or manually: |
| 68 | +pip3 install pypdf Pillow reportlab |
| 69 | +``` |
| 70 | + |
| 71 | +**Docker:** |
| 72 | +```bash |
| 73 | +# Dependencies already included in Docker image |
| 74 | +make docker-pdf |
| 75 | +``` |
| 76 | + |
| 77 | +## Customization |
| 78 | + |
| 79 | +### Using a Different Cover Image |
| 80 | +1. Replace `chapters/media/TS_SysmonCommunityGuide_Cover.png` with your image |
| 81 | +2. Image can be any size - will be automatically scaled to fit A4 |
| 82 | +3. Maintains aspect ratio - will not distort |
| 83 | +4. Centered on page |
| 84 | + |
| 85 | +### Disabling Cover Replacement |
| 86 | +If you want to use the default Pandoc-generated title page: |
| 87 | +1. Remove or rename the cover image file, OR |
| 88 | +2. Comment out the cover replacement section in `Build/md2pdf.sh` (lines 64-80) |
| 89 | + |
| 90 | +## Technical Details |
| 91 | + |
| 92 | +### Image Processing |
| 93 | +- Cover image is converted to a temporary PDF (`/tmp/cover_page.pdf`) |
| 94 | +- Image is scaled to fit A4 (210mm × 297mm) while maintaining aspect ratio |
| 95 | +- Image is centered on the page |
| 96 | +- Uses high-quality rendering from Pillow |
| 97 | + |
| 98 | +### PDF Manipulation |
| 99 | +- Original PDF is temporarily renamed |
| 100 | +- Cover page PDF is created with exact A4 dimensions |
| 101 | +- First page of original PDF is replaced with cover |
| 102 | +- All other pages (2-N) are copied unchanged |
| 103 | +- Temporary files are cleaned up |
| 104 | + |
| 105 | +### Error Handling |
| 106 | +- Checks if cover image exists before attempting replacement |
| 107 | +- Validates PDF was generated successfully |
| 108 | +- Falls back to original PDF if replacement fails |
| 109 | +- Provides clear error messages in build output |
| 110 | + |
| 111 | +## Verification |
| 112 | + |
| 113 | +After building, verify the cover replacement worked: |
| 114 | + |
| 115 | +```bash |
| 116 | +# Check PDF exists and has correct size |
| 117 | +ls -lh Build/SysmonGuide.pdf |
| 118 | + |
| 119 | +# Check number of pages |
| 120 | +file Build/SysmonGuide.pdf |
| 121 | + |
| 122 | +# Open PDF and visually inspect first page |
| 123 | +open Build/SysmonGuide.pdf # macOS |
| 124 | +xdg-open Build/SysmonGuide.pdf # Linux |
| 125 | +``` |
| 126 | + |
| 127 | +## Troubleshooting |
| 128 | + |
| 129 | +### "Module not found" errors |
| 130 | +Install Python dependencies: |
| 131 | +```bash |
| 132 | +pip3 install --user --break-system-packages pypdf Pillow reportlab |
| 133 | +``` |
| 134 | + |
| 135 | +### Cover replacement fails |
| 136 | +Check build log for errors: |
| 137 | +```bash |
| 138 | +cat Build/pdfgen.log |
| 139 | +``` |
| 140 | + |
| 141 | +### Cover image not found |
| 142 | +Verify the image exists: |
| 143 | +```bash |
| 144 | +ls -lh chapters/media/TS_SysmonCommunityGuide_Cover.png |
| 145 | +``` |
| 146 | + |
| 147 | +### PDF is too large |
| 148 | +The cover image adds ~3MB to the PDF. To reduce size: |
| 149 | +1. Optimize the cover image before building |
| 150 | +2. Convert to lower DPI (e.g., 150 DPI instead of 300 DPI) |
| 151 | +3. Use JPEG instead of PNG (with quality setting) |
| 152 | + |
| 153 | +## Future Enhancements |
| 154 | + |
| 155 | +Potential improvements: |
| 156 | +- [ ] Support for back cover page |
| 157 | +- [ ] Configurable cover image path in chapters.json |
| 158 | +- [ ] Multiple cover formats (PDF, SVG, etc.) |
| 159 | +- [ ] Automatic image optimization |
| 160 | +- [ ] Cover template system |
0 commit comments