rdesign/scripts/rswag_export.py

181 lines
5.4 KiB
Python

#!/usr/bin/env python3
"""
Scribus script: Create a print-ready PDF from an rSwag design image.
Generates a new Scribus document with the design placed on a page with
optional bleed and crop marks.
Called via: scribus -g -ns -py rswag_export.py -- [args]
"""
import sys
import os
# Paper sizes in mm (width, height)
PAPER_SIZES = {
"A4": (210, 297),
"A3": (297, 420),
"A5": (148, 210),
"Letter": (215.9, 279.4),
"Tabloid": (279.4, 431.8),
}
BLEED_MM = 3.0
CROP_MARK_LEN = 5.0
CROP_MARK_OFFSET = 3.0
def parse_args():
args = {"bleed": False, "crop_marks": False}
argv = sys.argv[1:]
i = 0
while i < len(argv):
if argv[i] == "--image" and i + 1 < len(argv):
args["image"] = argv[i + 1]
i += 2
elif argv[i] == "--output" and i + 1 < len(argv):
args["output"] = argv[i + 1]
i += 2
elif argv[i] == "--paper" and i + 1 < len(argv):
args["paper"] = argv[i + 1]
i += 2
elif argv[i] == "--dpi" and i + 1 < len(argv):
args["dpi"] = int(argv[i + 1])
i += 2
elif argv[i] == "--title" and i + 1 < len(argv):
args["title"] = argv[i + 1]
i += 2
elif argv[i] == "--bleed":
args["bleed"] = True
i += 1
elif argv[i] == "--crop-marks":
args["crop_marks"] = True
i += 1
else:
i += 1
return args
def main():
try:
import scribus
except ImportError:
print("ERROR: Must run inside Scribus")
sys.exit(1)
args = parse_args()
image_path = args.get("image")
output_path = args.get("output")
paper = args.get("paper", "A4")
dpi = args.get("dpi", 300)
title = args.get("title", "rSwag Design")
if not image_path or not output_path:
print("ERROR: --image and --output required")
sys.exit(1)
if not os.path.exists(image_path):
print(f"ERROR: Image not found: {image_path}")
sys.exit(1)
# Get paper dimensions
paper_w, paper_h = PAPER_SIZES.get(paper, PAPER_SIZES["A4"])
# Calculate page size with bleed
bleed = BLEED_MM if args["bleed"] else 0
page_w = paper_w + (bleed * 2)
page_h = paper_h + (bleed * 2)
# Create new document (dimensions in mm, SCRIBUS_UNIT_MILLIMETERS = 1)
scribus.newDocument(
(page_w, page_h), # page size
(10, 10, 10, 10), # margins (left, right, top, bottom)
scribus.PORTRAIT,
1, # first page number
scribus.UNIT_MILLIMETERS,
scribus.PAGE_1, # single page facing
0, # first page left
1, # number of pages
)
# Place the image centered on the page
# Calculate placement: center the image on the page
margin = 15 # mm margin around image
img_area_w = paper_w - (margin * 2)
img_area_h = paper_h - (margin * 2)
img_x = bleed + margin
img_y = bleed + margin
# Create image frame
img_frame = scribus.createImage(img_x, img_y, img_area_w, img_area_h, "design")
scribus.loadImage(image_path, img_frame)
# Scale image to fit frame proportionally
scribus.setScaleImageToFrame(True, True, img_frame)
# Add title text below the image
title_y = bleed + margin + img_area_h + 5
title_frame = scribus.createText(
bleed + margin, title_y,
img_area_w, 10,
"title"
)
scribus.setText(title, title_frame)
scribus.setFontSize(12, title_frame)
scribus.setTextAlignment(scribus.ALIGN_CENTERED, title_frame)
# Add crop marks if requested
if args["crop_marks"]:
mark_color = "Registration"
# Ensure Registration color exists (it's a default Scribus color)
line_width = 0.25 # mm
# Top-left corner
for mark_args in [
# Top-left
(bleed - CROP_MARK_OFFSET - CROP_MARK_LEN, bleed, CROP_MARK_LEN, 0),
(bleed, bleed - CROP_MARK_OFFSET - CROP_MARK_LEN, 0, CROP_MARK_LEN),
# Top-right
(bleed + paper_w + CROP_MARK_OFFSET, bleed, CROP_MARK_LEN, 0),
(bleed + paper_w, bleed - CROP_MARK_OFFSET - CROP_MARK_LEN, 0, CROP_MARK_LEN),
# Bottom-left
(bleed - CROP_MARK_OFFSET - CROP_MARK_LEN, bleed + paper_h, CROP_MARK_LEN, 0),
(bleed, bleed + paper_h + CROP_MARK_OFFSET, 0, CROP_MARK_LEN),
# Bottom-right
(bleed + paper_w + CROP_MARK_OFFSET, bleed + paper_h, CROP_MARK_LEN, 0),
(bleed + paper_w, bleed + paper_h + CROP_MARK_OFFSET, 0, CROP_MARK_LEN),
]:
x, y, w, h = mark_args
if w > 0:
line = scribus.createLine(x, y, x + w, y)
else:
line = scribus.createLine(x, y, x, y + h)
scribus.setLineWidth(line_width, line)
# Export to PDF
pdf = scribus.PDFfile()
pdf.file = output_path
pdf.quality = 0 # Maximum
pdf.resolution = dpi
pdf.version = 14 # PDF 1.4
pdf.compress = True
pdf.compressmtd = 0
if args["bleed"]:
pdf.useDocBleeds = False
pdf.bleedt = BLEED_MM
pdf.bleedb = BLEED_MM
pdf.bleedl = BLEED_MM
pdf.bleedr = BLEED_MM
if args["crop_marks"]:
pdf.cropMarks = True
pdf.markLength = CROP_MARK_LEN
pdf.markOffset = CROP_MARK_OFFSET
pdf.save()
scribus.closeDoc()
print(f"Exported: {output_path}")
if __name__ == "__main__":
main()