🔧 more options for PDF export

This commit is contained in:
TC
2026-06-21 19:10:24 +10:00
parent 8c10ba7731
commit f205332d24
4 changed files with 120 additions and 26 deletions

View File

@@ -391,8 +391,13 @@ export function registerIpcHandlers(): void {
showChapterTitle: boolean
includeCover: boolean
includeFrontMatter: boolean
includePartSeparators: boolean
pageFrom: number | null
pageTo: number | null
font: 'courier' | 'times' | 'georgia'
pageSize: 'letter' | 'a4'
titleOnFirstPage: boolean
boldHeadings: boolean
}): Promise<void> => {
const win = BrowserWindow.fromWebContents(event.sender)
const projectCfg = await readProjectConfig()
@@ -438,10 +443,18 @@ export function registerIpcHandlers(): void {
authorEmail,
].filter(Boolean)
const fontFamily = opts.font === 'times'
? '"Times New Roman", Times, serif'
: opts.font === 'georgia'
? 'Georgia, serif'
: '"Courier New", Courier, monospace'
const pageSizeCss = opts.pageSize === 'a4' ? 'A4' : 'letter'
const headingWeight = opts.boldHeadings ? 'bold' : 'normal'
const coverCSS = `
@page { size: letter; }
@page { size: ${pageSizeCss}; }
body {
font-family: "Courier New", Courier, monospace;
font-family: ${fontFamily};
font-size: 12pt;
line-height: 1.5;
color: #000;
@@ -509,6 +522,7 @@ export function registerIpcHandlers(): void {
// Body chapters = docs inside a Part subdirectory
let currentPart: string | null = null
let chapterIndex = 0
let firstChapterDone = false
for (const doc of docs) {
const slashIdx = doc.relativePath.indexOf('/')
@@ -517,14 +531,8 @@ export function registerIpcHandlers(): void {
if (isFrontMatter && !opts.includeFrontMatter) continue
if (partName !== null && partName !== currentPart) {
currentPart = partName
const safe = partName.replace(/&/g, '&amp;').replace(/</g, '&lt;')
sections.push({
title: partName,
bodyHtml: `<div class="part-page"><div class="part-title">${safe}</div></div>`
})
}
const isNewPart = partName !== null && partName !== currentPart
if (isNewPart) currentPart = partName
const fileName = doc.relativePath.split('/').pop()?.replace(/\.md$/, '') ?? doc.relativePath
let headerTitle: string
@@ -552,17 +560,26 @@ export function registerIpcHandlers(): void {
headerTitle = fileName
}
const contentHtml = await marked(normalize(doc.content))
let titleHtml = ''
if (!firstChapterDone && opts.titleOnFirstPage) {
titleHtml = `<h1 style="font-weight:bold;margin-bottom:0">${esc(projectName)}</h1>`
}
firstChapterDone = true
const partHtml = (isNewPart && opts.includePartSeparators && currentPart)
? `<h2 class="part-heading">${esc(currentPart)}</h2>`
: ''
sections.push({
title: headerTitle,
bodyHtml: `<div class="chapter">${headingHtml}${contentHtml}</div>`
bodyHtml: `<div class="chapter">${titleHtml}${partHtml}${headingHtml}${contentHtml}</div>`
})
}
// CSS shared by chapter/part sections — cover page uses its own CSS (set above).
const pageCSS = `
@page { size: letter; }
body { font-family: "Courier New", Courier, monospace; font-size: 12pt; line-height: 2; color: #000; margin: 0; padding: 0; }
h1, h2, h3 { font-weight: normal; font-size: 12pt; text-align: center; text-transform: uppercase; margin: 0; line-height: 2; }
@page { size: ${pageSizeCss}; }
body { font-family: ${fontFamily}; font-size: 12pt; line-height: 2; color: #000; margin: 0; padding: 0; }
h1 { font-weight: bold; font-size: 14pt; text-align: center; text-transform: uppercase; margin: 0; line-height: 2; }
h2, h3 { font-weight: ${headingWeight}; font-size: 12pt; text-align: center; text-transform: uppercase; margin: 0; line-height: 2; }
p { margin: 0; text-indent: 0.5in; text-align: left; }
h1 + p, h2 + p, h3 + p, hr + p, .chapter > p:first-child { text-indent: 0; }
hr { border: none; margin: 0; height: 2em; text-align: center; }
@@ -573,8 +590,7 @@ export function registerIpcHandlers(): void {
ul, ol { margin: 0 0 0 0.5in; }
li { margin: 0; }
.chapter { padding-top: 2.5in; }
.part-page { padding-top: 3.5in; text-align: center; }
.part-title { font-family: "Courier New", Courier, monospace; font-size: 12pt; text-transform: uppercase; }
.part-heading { font-family: ${fontFamily}; font-weight: bold; font-size: 14pt; text-align: center; text-transform: uppercase; letter-spacing: 0.15em; margin: 0; line-height: 2; }
`
// Print each section to its own PDF buffer. Cover page uses its own CSS;
@@ -591,7 +607,7 @@ export function registerIpcHandlers(): void {
try {
await offscreen.loadFile(tempPath)
const buf = await offscreen.webContents.printToPDF({
pageSize: 'Letter',
pageSize: opts.pageSize === 'a4' ? 'A4' : 'Letter',
displayHeaderFooter: false,
margins: { marginType: 'custom', top: 1.0, bottom: 1.0, left: 1.0, right: 1.0 }
})
@@ -605,7 +621,8 @@ export function registerIpcHandlers(): void {
// Merge all section PDFs into one document, tracking which section each page
// belongs to so we can stamp the correct running header on it.
const mergedPdf = await PDFDocument.create()
const courier = await mergedPdf.embedFont(StandardFonts.Courier)
const headerStandardFont = opts.font === 'courier' ? StandardFonts.Courier : StandardFonts.TimesRoman
const headerFont = await mergedPdf.embedFont(headerStandardFont)
// pageOwners[i] tracks the section title and whether the page is a cover page
const pageOwners: { title: string; isCover: boolean }[] = []
@@ -630,12 +647,12 @@ export function registerIpcHandlers(): void {
const chapterUpper = pageOwners[i].title.toUpperCase()
const headerText = `${projectTitle} / ${chapterUpper} / ${bodyPageNum}`
const fontSize = 11
const textWidth = courier.widthOfTextAtSize(headerText, fontSize)
const textWidth = headerFont.widthOfTextAtSize(headerText, fontSize)
page.drawText(headerText, {
x: width - 72 - textWidth, // 1 in from right edge (72pt = 1in)
y: height - 36, // 0.5 in from top edge (36pt = 0.5in)
size: fontSize,
font: courier,
font: headerFont,
color: rgb(0, 0, 0)
})
}

View File

@@ -35,6 +35,15 @@
grid-column: 2;
}
.export-format-row {
display: flex;
gap: 12px;
}
.export-format-field {
flex: 1;
}
.export-page-range {
display: flex;
align-items: center;

View File

@@ -6,18 +6,23 @@ export interface ExportOptions {
showChapterTitle: boolean
includeCover: boolean
includeFrontMatter: boolean
includePartSeparators: boolean
pageFrom: number | null
pageTo: number | null
font: 'courier' | 'times' | 'georgia'
pageSize: 'letter' | 'a4'
titleOnFirstPage: boolean
boldHeadings: boolean
}
const STORAGE_KEY = 'exportDialogPrefs'
function loadPrefs(): Pick<ExportOptions, 'romanNumerals' | 'showChapterTitle' | 'includeCover' | 'includeFrontMatter'> {
function loadPrefs(): Pick<ExportOptions, 'romanNumerals' | 'showChapterTitle' | 'includeCover' | 'includeFrontMatter' | 'includePartSeparators' | 'font' | 'pageSize' | 'titleOnFirstPage' | 'boldHeadings'> {
try {
const raw = localStorage.getItem(STORAGE_KEY)
if (raw) return JSON.parse(raw)
if (raw) return { font: 'courier', pageSize: 'letter', titleOnFirstPage: false, boldHeadings: false, includePartSeparators: true, ...JSON.parse(raw) }
} catch { /* ignore */ }
return { romanNumerals: true, showChapterTitle: false, includeCover: true, includeFrontMatter: true }
return { romanNumerals: true, showChapterTitle: false, includeCover: true, includeFrontMatter: true, includePartSeparators: true, font: 'courier', pageSize: 'letter', titleOnFirstPage: false, boldHeadings: false }
}
interface Props {
@@ -31,6 +36,11 @@ export function ExportDialog({ onClose, onExport }: Props): JSX.Element {
const [showChapterTitle, setShowChapterTitle] = useState(prefs.showChapterTitle)
const [includeCover, setIncludeCover] = useState(prefs.includeCover)
const [includeFrontMatter, setIncludeFrontMatter] = useState(prefs.includeFrontMatter)
const [includePartSeparators, setIncludePartSeparators] = useState(prefs.includePartSeparators)
const [font, setFont] = useState(prefs.font)
const [pageSize, setPageSize] = useState(prefs.pageSize)
const [titleOnFirstPage, setTitleOnFirstPage] = useState(prefs.titleOnFirstPage)
const [boldHeadings, setBoldHeadings] = useState(prefs.boldHeadings)
const [pageFrom, setPageFrom] = useState('')
const [pageTo, setPageTo] = useState('')
const overlayRef = useRef<HTMLDivElement>(null)
@@ -48,14 +58,14 @@ export function ExportDialog({ onClose, onExport }: Props): JSX.Element {
}
const savePrefs = (patch: Partial<typeof prefs>): void => {
const next = { romanNumerals, showChapterTitle, includeCover, includeFrontMatter, ...patch }
const next = { romanNumerals, showChapterTitle, includeCover, includeFrontMatter, includePartSeparators, font, pageSize, titleOnFirstPage, boldHeadings, ...patch }
localStorage.setItem(STORAGE_KEY, JSON.stringify(next))
}
const handleExport = (): void => {
const from = pageFrom.trim() ? parseInt(pageFrom, 10) : null
const to = pageTo.trim() ? parseInt(pageTo, 10) : null
onExport({ romanNumerals, showChapterTitle, includeCover, includeFrontMatter, pageFrom: from, pageTo: to })
onExport({ romanNumerals, showChapterTitle, includeCover, includeFrontMatter, includePartSeparators, pageFrom: from, pageTo: to, font, pageSize, titleOnFirstPage, boldHeadings })
}
const pageRangeValid = (): boolean => {
@@ -116,6 +126,64 @@ export function ExportDialog({ onClose, onExport }: Props): JSX.Element {
<span className="export-toggle-label">Include front &amp; back matter</span>
<span className="export-toggle-hint">Prologue, Content Warning, Epilogue</span>
</label>
<label className="export-toggle">
<input
type="checkbox"
checked={includePartSeparators}
onChange={e => { setIncludePartSeparators(e.target.checked); savePrefs({ includePartSeparators: e.target.checked }) }}
/>
<span className="export-toggle-label">Include part labels</span>
<span className="export-toggle-hint">Part name above the first chapter of each part</span>
</label>
</div>
<div className="export-format-row">
<div className="settings-field export-format-field">
<span className="settings-label">Font</span>
<select
className="settings-input"
value={font}
onChange={e => { const v = e.target.value as ExportOptions['font']; setFont(v); savePrefs({ font: v }) }}
>
<option value="courier">Courier New</option>
<option value="times">Times New Roman</option>
<option value="georgia">Georgia</option>
</select>
</div>
<div className="settings-field export-format-field">
<span className="settings-label">Page size</span>
<select
className="settings-input"
value={pageSize}
onChange={e => { const v = e.target.value as ExportOptions['pageSize']; setPageSize(v); savePrefs({ pageSize: v }) }}
>
<option value="letter">Letter</option>
<option value="a4">A4</option>
</select>
</div>
</div>
<div className="export-toggles">
<label className="export-toggle">
<input
type="checkbox"
checked={titleOnFirstPage}
onChange={e => { setTitleOnFirstPage(e.target.checked); savePrefs({ titleOnFirstPage: e.target.checked }) }}
/>
<span className="export-toggle-label">Title on first page</span>
<span className="export-toggle-hint">Manuscript title bold and centered above chapter one</span>
</label>
<label className="export-toggle">
<input
type="checkbox"
checked={boldHeadings}
onChange={e => { setBoldHeadings(e.target.checked); savePrefs({ boldHeadings: e.target.checked }) }}
/>
<span className="export-toggle-label">Bold chapter headings</span>
<span className="export-toggle-hint">Chapter numbers and titles in bold</span>
</label>
</div>
<div className="settings-field">

File diff suppressed because one or more lines are too long