Bugfix: Umlaute falsch in den PDFs

This commit is contained in:
concierge committed 2026-11-16 15:54:44 +01:00
1 parent 66659d9a23
commit b3afa3ef32
3 files changed
+175 -27

No files matched your search

+1 -1
View File
@@ -166,7 +166,7 @@ func Cover(w io.Writer, data CoverData) error {
} }
for index, column := range columns { for index, column := range columns {
pdf.SetXY(x, y+2.1) pdf.SetXY(x, y+2.1)
pdf.CellFormat(column.Width-2, 3.8, tr(fit(pdf, values[index], column.Width-2)), "", 0, "L", false, 0, "") pdf.CellFormat(column.Width-2, 3.8, fit(pdf, tr, values[index], column.Width-2), "", 0, "L", false, 0, "")
x += column.Width x += column.Width
} }
y += rowHeight y += rowHeight
+40 -26
View File
@@ -108,7 +108,10 @@ func hexColor(value string, fallback [3]int) [3]int {
return [3]int{int(parsed >> 16 & 0xFF), int(parsed >> 8 & 0xFF), int(parsed & 0xFF)} return [3]int{int(parsed >> 16 & 0xFF), int(parsed >> 8 & 0xFF), int(parsed & 0xFF)}
} }
func fit(pdf *fpdf.Fpdf, text string, width float64) string { // fitShorten truncates already encoded text to width. Both text and ellipsis
// must already be encoded for the current font, because core fonts are
// single-byte code pages and widths are measured per output byte.
func fitShorten(pdf *fpdf.Fpdf, text string, width float64, ellipsis string) string {
text = strings.TrimSpace(text) text = strings.TrimSpace(text)
if text == "" { if text == "" {
return "" return ""
@@ -116,10 +119,9 @@ func fit(pdf *fpdf.Fpdf, text string, width float64) string {
if pdf.GetStringWidth(text) <= width { if pdf.GetStringWidth(text) <= width {
return text return text
} }
runes := []rune(text) for len(text) > 0 {
for len(runes) > 0 { text = strings.TrimSpace(text[:len(text)-1])
runes = runes[:len(runes)-1] candidate := text + ellipsis
candidate := strings.TrimSpace(string(runes)) + "…"
if pdf.GetStringWidth(candidate) <= width { if pdf.GetStringWidth(candidate) <= width {
return candidate return candidate
} }
@@ -127,8 +129,18 @@ func fit(pdf *fpdf.Fpdf, text string, width float64) string {
return "" return ""
} }
// wrap splits text into at most maxLines lines fitting width. // fit translates text to the PDF code page and truncates it to width.
func wrap(pdf *fpdf.Fpdf, text string, width float64, maxLines int) []string { func fit(pdf *fpdf.Fpdf, tr func(string) string, text string, width float64) string {
return fitShorten(pdf, tr(text), width, tr("…"))
}
// wrap splits text into at most maxLines lines fitting width and returns them
// already encoded for the current font.
func wrap(pdf *fpdf.Fpdf, tr func(string) string, text string, width float64, maxLines int) []string {
return wrapEncoded(pdf, tr(text), width, maxLines, tr("…"))
}
func wrapEncoded(pdf *fpdf.Fpdf, text string, width float64, maxLines int, ellipsis string) []string {
text = strings.TrimSpace(text) text = strings.TrimSpace(text)
if text == "" || maxLines < 1 { if text == "" || maxLines < 1 {
return nil return nil
@@ -152,17 +164,19 @@ func wrap(pdf *fpdf.Fpdf, text string, width float64, maxLines int) []string {
} }
current = word current = word
} else { } else {
current = fit(pdf, word, width) current = fitShorten(pdf, word, width, ellipsis)
} }
} }
if len(lines) < maxLines && current != "" { if len(lines) < maxLines && current != "" {
lines = append(lines, current) lines = append(lines, current)
} else if len(lines) == maxLines && current != "" { } else if len(lines) == maxLines && current != "" {
lines[len(lines)-1] = fit(pdf, lines[len(lines)-1]+" "+current, width) lines[len(lines)-1] = fitShorten(pdf, lines[len(lines)-1]+" "+current, width, ellipsis)
} }
return lines return lines
} }
// writeLines writes lines that are already encoded for the current font; do
// not pass raw UTF-8 text here.
func writeLines(pdf *fpdf.Fpdf, lines []string, x, y, width, lineHeight float64) float64 { func writeLines(pdf *fpdf.Fpdf, lines []string, x, y, width, lineHeight float64) float64 {
for _, line := range lines { for _, line := range lines {
pdf.SetXY(x, y) pdf.SetXY(x, y)
@@ -327,11 +341,11 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
setColor(pdf, color) setColor(pdf, color)
pdf.SetFont("Times", "B", 8.2) pdf.SetFont("Times", "B", 8.2)
pdf.SetXY(x+1.3, y+1.2) pdf.SetXY(x+1.3, y+1.2)
pdf.CellFormat(churchWidth-2.6, 4, tr(fit(pdf, church.Name, churchWidth-2.6)), "", 0, "L", false, 0, "") pdf.CellFormat(churchWidth-2.6, 4, fit(pdf, tr, church.Name, churchWidth-2.6), "", 0, "L", false, 0, "")
setColor(pdf, colors.tertiary) setColor(pdf, colors.tertiary)
pdf.SetFont("Helvetica", "", 5.2) pdf.SetFont("Helvetica", "", 5.2)
pdf.SetXY(x+1.3, y+5.1) pdf.SetXY(x+1.3, y+5.1)
pdf.CellFormat(churchWidth-2.6, 2.6, tr(fit(pdf, church.Address, churchWidth-2.6)), "", 0, "L", false, 0, "") pdf.CellFormat(churchWidth-2.6, 2.6, fit(pdf, tr, church.Address, churchWidth-2.6), "", 0, "L", false, 0, "")
x += churchWidth + gap x += churchWidth + gap
} }
setDraw(pdf, colors.line) setDraw(pdf, colors.line)
@@ -355,10 +369,10 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
rowHeight := func(row domain.DayPlanRow) float64 { rowHeight := func(row domain.DayPlanRow) float64 {
height := 15.0 height := 15.0
if row.LiturgicalDay != nil { if row.LiturgicalDay != nil {
titles := wrap(pdf, row.LiturgicalDay.DisplayTitle(), liturgyW-4, 3) titles := wrap(pdf, tr, row.LiturgicalDay.DisplayTitle(), liturgyW-4, 3)
readings := []string{} readings := []string{}
if !row.LiturgicalDay.Readings.IsEmpty() { if !row.LiturgicalDay.Readings.IsEmpty() {
readings = wrap(pdf, row.LiturgicalDay.Readings.ShortLabel(), liturgyW-4, 3) readings = wrap(pdf, tr, row.LiturgicalDay.Readings.ShortLabel(), liturgyW-4, 3)
} }
lit := 4.5 + float64(len(titles))*3.4 + float64(len(readings))*2.5 lit := 4.5 + float64(len(titles))*3.4 + float64(len(readings))*2.5
if badge, _ := feastBadge(row.LiturgicalDay); badge != "" { if badge, _ := feastBadge(row.LiturgicalDay); badge != "" {
@@ -472,12 +486,12 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
fontStyle = "" fontStyle = ""
} }
pdf.SetFont("Times", fontStyle, 8.2) pdf.SetFont("Times", fontStyle, 8.2)
titleLines := wrap(pdf, day.DisplayTitle(), liturgyW-4, 3) titleLines := wrap(pdf, tr, day.DisplayTitle(), liturgyW-4, 3)
textY = writeLines(pdf, titleLines, x+1.2, textY, liturgyW-4, 3.3) textY = writeLines(pdf, titleLines, x+1.2, textY, liturgyW-4, 3.3)
if !day.Readings.IsEmpty() { if !day.Readings.IsEmpty() {
setColor(pdf, colors.secondary) setColor(pdf, colors.secondary)
pdf.SetFont("Helvetica", "", 5.6) pdf.SetFont("Helvetica", "", 5.6)
readingLines := wrap(pdf, day.Readings.ShortLabel(), liturgyW-4, 3) readingLines := wrap(pdf, tr, day.Readings.ShortLabel(), liturgyW-4, 3)
writeLines(pdf, readingLines, x+1.2, textY+0.4, liturgyW-4, 2.5) writeLines(pdf, readingLines, x+1.2, textY+0.4, liturgyW-4, 2.5)
} }
} }
@@ -491,15 +505,15 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
for _, service := range services { for _, service := range services {
setColor(pdf, color) setColor(pdf, color)
pdf.SetFont("Helvetica", "B", 6.2) pdf.SetFont("Helvetica", "B", 6.2)
label := fit(pdf, service.Service.TimeLabel()+" "+service.Service.Title, churchWidth-4) label := fit(pdf, tr, service.Service.TimeLabel()+" "+service.Service.Title, churchWidth-4)
pdf.SetXY(x+1.2, serviceY) pdf.SetXY(x+1.2, serviceY)
pdf.CellFormat(churchWidth-2.4, 3.6, tr(label), "", 0, "L", false, 0, "") pdf.CellFormat(churchWidth-2.4, 3.6, label, "", 0, "L", false, 0, "")
serviceY += 3.4 serviceY += 3.4
if service.Service.Location != nil && strings.TrimSpace(*service.Service.Location) != "" { if service.Service.Location != nil && strings.TrimSpace(*service.Service.Location) != "" {
setColor(pdf, colors.burgundy) setColor(pdf, colors.burgundy)
pdf.SetFont("Helvetica", "B", 5.4) pdf.SetFont("Helvetica", "B", 5.4)
pdf.SetXY(x+1.6, serviceY) pdf.SetXY(x+1.6, serviceY)
pdf.CellFormat(churchWidth-3, 2.8, tr(fit(pdf, *service.Service.Location, churchWidth-3)), "", 0, "L", false, 0, "") pdf.CellFormat(churchWidth-3, 2.8, fit(pdf, tr, *service.Service.Location, churchWidth-3), "", 0, "L", false, 0, "")
serviceY += 3.0 serviceY += 3.0
} }
for _, role := range domain.AllRoles { for _, role := range domain.AllRoles {
@@ -519,7 +533,7 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
setColor(pdf, colors.secondary) setColor(pdf, colors.secondary)
pdf.SetFont("Helvetica", "", 5.4) pdf.SetFont("Helvetica", "", 5.4)
} }
lines := wrap(pdf, text, churchWidth-3, 2) lines := wrap(pdf, tr, text, churchWidth-3, 2)
serviceY = writeLines(pdf, lines, x+1.6, serviceY, churchWidth-3, 3.0) serviceY = writeLines(pdf, lines, x+1.6, serviceY, churchWidth-3, 3.0)
} }
serviceY += 1.5 serviceY += 1.5
@@ -652,8 +666,8 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
} }
lectorNames, _ := namesFor(service, domain.RoleLector, peopleByID) lectorNames, _ := namesFor(service, domain.RoleLector, peopleByID)
helperNames, _ := namesFor(service, domain.RoleHelper, peopleByID) helperNames, _ := namesFor(service, domain.RoleHelper, peopleByID)
lectorLines := len(wrap(pdf, lectorNames, lectorW-2, 2)) lectorLines := len(wrap(pdf, tr, lectorNames, lectorW-2, 2))
helperLines := len(wrap(pdf, helperNames, helperW-2, 2)) helperLines := len(wrap(pdf, tr, helperNames, helperW-2, 2))
if lines := maxInt(lectorLines, helperLines); float64(lines)*3 > height { if lines := maxInt(lectorLines, helperLines); float64(lines)*3 > height {
height = float64(lines) * 3 height = float64(lines) * 3
} }
@@ -727,7 +741,7 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
if row.IsHighFeast() { if row.IsHighFeast() {
setColor(pdf, colors.gold) setColor(pdf, colors.gold)
pdf.SetFont("Helvetica", "B", 5.2) pdf.SetFont("Helvetica", "B", 5.2)
hint := wrap(pdf, feastHint(row.LiturgicalDay), dateW-4, 3) hint := wrap(pdf, tr, feastHint(row.LiturgicalDay), dateW-4, 3)
writeLines(pdf, hint, margin+1.5, y+10, dateW-4, 2.4) writeLines(pdf, hint, margin+1.5, y+10, dateW-4, 2.4)
} }
} }
@@ -737,9 +751,9 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
setColor(pdf, churchColor) setColor(pdf, churchColor)
pdf.SetFont("Helvetica", "B", 7.2) pdf.SetFont("Helvetica", "B", 7.2)
place := service.Service.PlaceName(service.Church) place := service.Service.PlaceName(service.Church)
line := fit(pdf, service.Service.TimeLabel()+" · "+place, serviceW-2) line := fit(pdf, tr, service.Service.TimeLabel()+" · "+place, serviceW-2)
pdf.SetXY(x, y+1.6) pdf.SetXY(x, y+1.6)
pdf.CellFormat(serviceW-2, 3.4, tr(line), "", 0, "L", false, 0, "") pdf.CellFormat(serviceW-2, 3.4, line, "", 0, "L", false, 0, "")
if service.Service.Title != "" { if service.Service.Title != "" {
setColor(pdf, colors.secondary) setColor(pdf, colors.secondary)
if service.Service.Kind == domain.KindSpecial { if service.Service.Kind == domain.KindSpecial {
@@ -751,7 +765,7 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
title += " · Sondergottesdienst" title += " · Sondergottesdienst"
} }
pdf.SetXY(x, y+4.8) pdf.SetXY(x, y+4.8)
pdf.CellFormat(serviceW-2, 3, tr(fit(pdf, title, serviceW-2)), "", 0, "L", false, 0, "") pdf.CellFormat(serviceW-2, 3, fit(pdf, tr, title, serviceW-2), "", 0, "L", false, 0, "")
} }
x += serviceW + gap x += serviceW + gap
@@ -772,7 +786,7 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
setColor(pdf, colors.ink) setColor(pdf, colors.ink)
pdf.SetFont("Helvetica", "", 6.6) pdf.SetFont("Helvetica", "", 6.6)
} }
lines := wrap(pdf, names, width, 2) lines := wrap(pdf, tr, names, width, 2)
writeLines(pdf, lines, x, y+1.8, width, 3) writeLines(pdf, lines, x, y+1.8, width, 3)
} }
drawRole(domain.RoleLector, lectorW) drawRole(domain.RoleLector, lectorW)
+134
View File
@@ -2,6 +2,8 @@ package pdf
import ( import (
"bytes" "bytes"
"compress/zlib"
"io"
"testing" "testing"
"time" "time"
@@ -33,6 +35,138 @@ func sampleRows() (domain.DayRange, []domain.DayPlanRow, []domain.Church, []doma
return r, []domain.DayPlanRow{row}, []domain.Church{church}, []domain.Person{person} return r, []domain.DayPlanRow{row}, []domain.Church{church}, []domain.Person{person}
} }
func umlautRows() (domain.DayRange, []domain.DayPlanRow, []domain.Church, []domain.Person) {
location := "Mariä-Himmelfahrt-Kapelle"
church := domain.Church{ID: "c1", Name: "St. Mariä Himmelfahrt", ShortName: "Mariä",
Symbol: domain.SymbolBasilica, ColorHex: "#9B5B44", Address: "Andreasstraße 1", SortIndex: 0}
person := domain.Person{ID: "p1", FirstName: "Jürgen", LastName: "Müller",
Roles: []domain.Role{domain.RoleLector}, Active: true}
service := domain.ServiceOccurrence{
ID: "s1", ChurchID: "c1", Date: domain.MustDate("2026-11-15"),
TimeMinutes: 9*60 + 30, Title: "Hochamt für die Gemeinde", Location: &location,
LectorsNeeded: 1, HelpersNeeded: 1, Kind: domain.KindRegular, CreatedAt: time.Now(),
}
assignment := domain.Assignment{ID: "a1", ServiceID: "s1", PersonID: "p1", Role: domain.RoleLector}
liturgy := domain.LiturgicalDay{
Date: domain.MustDate("2026-11-15"), Title: "Hochfest Mariä Himmelfahrt",
Rank: domain.RankSolemnity, Color: domain.ColorWhite,
Readings: domain.Readings{First: "Jes 61, 9a.10b", Gospel: "Lk 1, 39-56"},
IsObligation: true, Source: "test",
}
row := domain.DayPlanRow{
Date: domain.MustDate("2026-11-15"), LiturgicalDay: &liturgy,
Services: []domain.ServiceWithAssignments{{Service: service, Church: church, Assignments: []domain.Assignment{assignment}}},
}
r := domain.NewDayRange(domain.MustDate("2026-11-15"), domain.MustDate("2026-11-15"))
return r, []domain.DayPlanRow{row}, []domain.Church{church}, []domain.Person{person}
}
// pdfContentStreams returns the decoded page content streams of a PDF. Text
// written with core fonts is stored there as single-byte cp1252 strings.
func pdfContentStreams(t *testing.T, data []byte) []byte {
t.Helper()
var out bytes.Buffer
for {
open := bytes.Index(data, []byte("stream"))
if open < 0 {
break
}
start := open + len("stream")
if start < len(data) && data[start] == '\r' {
start++
}
if start < len(data) && data[start] == '\n' {
start++
}
end := bytes.Index(data[start:], []byte("endstream"))
if end < 0 {
break
}
raw := bytes.TrimRight(data[start:start+end], "\r\n")
if reader, err := zlib.NewReader(bytes.NewReader(raw)); err == nil {
decoded, err := io.ReadAll(reader)
reader.Close()
if err == nil {
out.Write(decoded)
out.WriteByte('\n')
}
} else {
out.Write(raw)
out.WriteByte('\n')
}
data = data[start+end+len("endstream"):]
}
if out.Len() == 0 {
t.Fatal("no content streams found in PDF")
}
return out.Bytes()
}
func TestPDFUmlautsAreEncoded(t *testing.T) {
r, rows, churches, people := umlautRows()
person := people[0]
cover := CoverData{
TenantName: "Köln", Diocese: "Erzbistum Köln", Range: r, Person: person,
Intro: "Grüße aus dem Pfarrbüro.",
Greeting: "Herzliche Grüße",
Assignments: []DispatchAssignment{{
Date: domain.MustDate("2026-11-15"), TimeMinutes: 9*60 + 30,
Church: "St. Mariä Himmelfahrt", Title: "Hochamt für die Gemeinde", Role: domain.RoleLector,
}},
GeneratedAt: time.Now(),
}
documents := []struct {
name string
render func(io.Writer) error
present []string
}{
{
name: "full",
render: func(w io.Writer) error {
return FullPlan(w, r, rows, churches, people, Options{TenantName: "Köln", Diocese: "Erzbistum Köln"})
},
present: []string{"K\xf6ln", "M\xfcller", "Andreasstra\xdfe", "Mari\xe4", "f\xfcr"},
},
{
name: "compact",
render: func(w io.Writer) error {
return CompactPlan(w, r, rows, people, Options{TenantName: "Köln", Diocese: "Erzbistum Köln"})
},
present: []string{"K\xd6LN", "M\xfcller", "Mari\xe4", "f\xfcr"},
},
{
name: "cover",
render: func(w io.Writer) error { return Cover(w, cover) },
present: []string{
"K\xf6ln", "M\xfcller", "Mari\xe4", "f\xfcr", "Gr\xfc\xdfe",
},
},
}
for _, document := range documents {
t.Run(document.name, func(t *testing.T) {
var buf bytes.Buffer
if err := document.render(&buf); err != nil {
t.Fatalf("render: %v", err)
}
content := pdfContentStreams(t, buf.Bytes())
for _, want := range document.present {
if !bytes.Contains(content, []byte(want)) {
t.Errorf("cp1252 sequence %q missing from PDF content", want)
}
}
for _, raw := range []string{
"K\xc3\xb6ln", "M\xc3\xbcller", "Andreasstra\xc3\x9fe", "Mari\xc3\xa4", "f\xc3\xbcr", "Gr\xc3\xbc\xc3\x9fe",
} {
if bytes.Contains(content, []byte(raw)) {
t.Errorf("raw UTF-8 sequence %q found in PDF content", raw)
}
}
})
}
}
func TestFullPlan(t *testing.T) { func TestFullPlan(t *testing.T) {
r, rows, churches, people := sampleRows() r, rows, churches, people := sampleRows()
var buf bytes.Buffer var buf bytes.Buffer