summaryrefslogtreecommitdiff
path: root/internal/comic/pdf.go
blob: 23a7e1176a1b8f42e7c720c6b7d05953011ee60d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package comic

import (
	"fmt"
	"os/exec"
	"path/filepath"
	"strings"
)

// AssembleComicPDF combines comic pages into a PDF using ImageMagick.
func AssembleComicPDF(outputDir, titleSlug string, imagePaths []string) (string, error) {
	if len(imagePaths) == 0 {
		return "", fmt.Errorf("no comic images to assemble into PDF")
	}
	if _, err := exec.LookPath("convert"); err != nil {
		return "", fmt.Errorf("ImageMagick 'convert' not found — install ImageMagick to generate the PDF")
	}

	pdfPath := filepath.Join(outputDir, titleSlug+".pdf")
	args := []string{"-density", "150"}
	args = append(args, imagePaths...)
	args = append(args, pdfPath)

	cmd := exec.Command("convert", args...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("convert failed: %w\n%s", err, strings.TrimSpace(string(out)))
	}
	return pdfPath, nil
}