1. Version Versendung
This commit is contained in:
1 parent
d7113afa5f
commit
781aa20253
31 files changed
+4023
-10
No files matched your search
@@ -0,0 +1,202 @@
|
||||
package pdf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lektorenplan/domain"
|
||||
)
|
||||
|
||||
// DispatchAssignment is one entry in a recipient's personal overview.
|
||||
type DispatchAssignment struct {
|
||||
Date time.Time
|
||||
TimeMinutes int
|
||||
Church string
|
||||
Title string
|
||||
Role domain.Role
|
||||
}
|
||||
|
||||
// CoverData describes the personal cover sheet of a dispatch packet.
|
||||
type CoverData struct {
|
||||
TenantName string
|
||||
Diocese string
|
||||
Range domain.DayRange
|
||||
Person domain.Person
|
||||
Intro string
|
||||
Greeting string
|
||||
Assignments []DispatchAssignment
|
||||
GeneratedAt time.Time
|
||||
}
|
||||
|
||||
// AssignmentLines returns the personal assignments as readable lines.
|
||||
func AssignmentLines(assignments []DispatchAssignment) []string {
|
||||
lines := make([]string, 0, len(assignments))
|
||||
for _, assignment := range assignments {
|
||||
lines = append(lines, fmt.Sprintf("%s · %s · %s · %s (%s)",
|
||||
domain.ShortWeekdayName(assignment.Date),
|
||||
domain.MediumString(assignment.Date),
|
||||
domain.TimeString(assignment.TimeMinutes),
|
||||
assignment.Church,
|
||||
assignment.Role.Title()))
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func coverOptions(data CoverData) Options {
|
||||
return Options{TenantName: data.TenantName, Diocese: data.Diocese, GeneratedAt: data.GeneratedAt}
|
||||
}
|
||||
|
||||
// Cover writes an A4 portrait cover letter with the personal assignment overview.
|
||||
func Cover(w io.Writer, data CoverData) error {
|
||||
pdf := newPDF("P")
|
||||
tr := translator(pdf)
|
||||
opts := coverOptions(data)
|
||||
|
||||
const (
|
||||
pageWidth = 210.0
|
||||
margin = 18.0
|
||||
contentW = pageWidth - margin*2
|
||||
)
|
||||
|
||||
pdf.AddPage()
|
||||
|
||||
// Header ---------------------------------------------------------------
|
||||
setColor(pdf, colors.accent)
|
||||
pdf.SetFont("Helvetica", "B", 7)
|
||||
pdf.SetXY(margin, margin)
|
||||
pdf.CellFormat(contentW, 4, tr("LEKTORENPLAN · "+strings.ToUpper(opts.tenant())), "", 0, "L", false, 0, "")
|
||||
|
||||
setColor(pdf, colors.ink)
|
||||
pdf.SetFont("Times", "B", 20)
|
||||
pdf.SetXY(margin, margin+6)
|
||||
pdf.CellFormat(contentW, 9, tr("Gottesdienstplan"), "", 0, "L", false, 0, "")
|
||||
|
||||
setColor(pdf, colors.secondary)
|
||||
pdf.SetFont("Helvetica", "", 10)
|
||||
pdf.SetXY(margin, margin+16)
|
||||
pdf.CellFormat(contentW, 5, tr(data.Range.LongTitle()), "", 0, "L", false, 0, "")
|
||||
|
||||
setDraw(pdf, colors.line)
|
||||
pdf.SetLineWidth(0.3)
|
||||
pdf.Line(margin, margin+24, pageWidth-margin, margin+24)
|
||||
|
||||
// Recipient ------------------------------------------------------------
|
||||
y := margin + 33
|
||||
setColor(pdf, colors.tertiary)
|
||||
pdf.SetFont("Helvetica", "B", 6.5)
|
||||
pdf.SetXY(margin, y)
|
||||
pdf.CellFormat(contentW, 3.5, tr("EMPFÄNGER"), "", 0, "L", false, 0, "")
|
||||
setColor(pdf, colors.ink)
|
||||
pdf.SetFont("Times", "B", 13)
|
||||
pdf.SetXY(margin, y+4.5)
|
||||
pdf.CellFormat(contentW, 6, tr(data.Person.FullName()), "", 0, "L", false, 0, "")
|
||||
if strings.TrimSpace(data.Person.Email) != "" {
|
||||
setColor(pdf, colors.secondary)
|
||||
pdf.SetFont("Helvetica", "", 8)
|
||||
pdf.SetXY(margin, y+10.5)
|
||||
pdf.CellFormat(contentW, 4, tr(data.Person.Email), "", 0, "L", false, 0, "")
|
||||
}
|
||||
|
||||
y += 22
|
||||
setColor(pdf, colors.tertiary)
|
||||
pdf.SetFont("Helvetica", "", 7)
|
||||
pdf.SetXY(margin, y)
|
||||
pdf.CellFormat(contentW, 4, tr("Erstellt am "+domain.MediumString(opts.generated())), "", 0, "R", false, 0, "")
|
||||
|
||||
// Letter text ----------------------------------------------------------
|
||||
y += 10
|
||||
setColor(pdf, colors.ink)
|
||||
pdf.SetFont("Times", "", 11.5)
|
||||
pdf.SetXY(margin, y)
|
||||
pdf.MultiCell(contentW, 5.6, tr(strings.TrimSpace(data.Intro)), "", "L", false)
|
||||
y = pdf.GetY() + 8
|
||||
|
||||
// Assignment table -----------------------------------------------------
|
||||
setColor(pdf, colors.ink)
|
||||
pdf.SetFont("Times", "B", 12.5)
|
||||
pdf.SetXY(margin, y)
|
||||
pdf.CellFormat(contentW, 6, tr("Ihre Einsätze im Planungszeitraum"), "", 0, "L", false, 0, "")
|
||||
y += 9
|
||||
|
||||
if len(data.Assignments) == 0 {
|
||||
setColor(pdf, colors.secondary)
|
||||
pdf.SetFont("Times", "", 10.5)
|
||||
pdf.SetXY(margin, y)
|
||||
pdf.MultiCell(contentW, 5.2, tr("Für Sie sind in diesem Zeitraum keine Einsätze eingetragen."), "", "L", false)
|
||||
y = pdf.GetY() + 8
|
||||
} else {
|
||||
columns := []struct {
|
||||
Label string
|
||||
Width float64
|
||||
}{
|
||||
{"Tag", 50}, {"Zeit", 20}, {"Ort", 44}, {"Dienst", contentW - 114},
|
||||
}
|
||||
setFill(pdf, colors.headerWash)
|
||||
pdf.Rect(margin, y, contentW, 7.5, "F")
|
||||
x := margin + 2
|
||||
setColor(pdf, colors.tertiary)
|
||||
pdf.SetFont("Helvetica", "B", 6.3)
|
||||
for _, column := range columns {
|
||||
pdf.SetXY(x, y+2.2)
|
||||
pdf.CellFormat(column.Width-2, 3.4, tr(strings.ToUpper(column.Label)), "", 0, "L", false, 0, "")
|
||||
x += column.Width
|
||||
}
|
||||
y += 7.5
|
||||
setDraw(pdf, colors.line)
|
||||
pdf.Line(margin, y, pageWidth-margin, y)
|
||||
|
||||
for _, assignment := range data.Assignments {
|
||||
if y > 252 {
|
||||
pdf.AddPage()
|
||||
y = margin
|
||||
}
|
||||
rowHeight := 8.2
|
||||
setColor(pdf, colors.ink)
|
||||
pdf.SetFont("Helvetica", "", 7.6)
|
||||
x = margin + 2
|
||||
dayLabel := fmt.Sprintf("%s, %d.%d.%d", domain.ShortWeekdayName(assignment.Date),
|
||||
domain.DayNumber(assignment.Date), domain.Month(assignment.Date), domain.Year(assignment.Date))
|
||||
values := []string{
|
||||
dayLabel,
|
||||
domain.TimeString(assignment.TimeMinutes),
|
||||
assignment.Church,
|
||||
assignment.Role.Title() + " · " + assignment.Title,
|
||||
}
|
||||
for index, column := range columns {
|
||||
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, "")
|
||||
x += column.Width
|
||||
}
|
||||
y += rowHeight
|
||||
setDraw(pdf, colors.line)
|
||||
pdf.SetLineWidth(0.15)
|
||||
pdf.Line(margin, y, pageWidth-margin, y)
|
||||
}
|
||||
y += 8
|
||||
}
|
||||
|
||||
// Greeting -------------------------------------------------------------
|
||||
if strings.TrimSpace(data.Greeting) != "" {
|
||||
if y > 245 {
|
||||
pdf.AddPage()
|
||||
y = margin
|
||||
}
|
||||
setColor(pdf, colors.ink)
|
||||
pdf.SetFont("Times", "", 11.5)
|
||||
pdf.SetXY(margin, y)
|
||||
pdf.MultiCell(contentW, 5.6, tr(strings.TrimSpace(data.Greeting)), "", "L", false)
|
||||
}
|
||||
|
||||
// Footer ---------------------------------------------------------------
|
||||
setColor(pdf, colors.tertiary)
|
||||
pdf.SetFont("Helvetica", "", 6.5)
|
||||
pdf.SetXY(margin, 284)
|
||||
pdf.CellFormat(contentW, 3.4, tr("LektorenPlan · "+opts.tenant()+" · "+data.Range.ShortTitle()), "", 0, "L", false, 0, "")
|
||||
|
||||
if err := pdf.Output(w); err != nil {
|
||||
return err
|
||||
}
|
||||
return pdf.Error()
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package pdf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/go-pdf/fpdf"
|
||||
"github.com/go-pdf/fpdf/contrib/gofpdi"
|
||||
)
|
||||
|
||||
// Merge concatenates PDF documents page by page, preserving page sizes and
|
||||
// orientation. It is used for printable dispatch packets that combine a cover
|
||||
// sheet with the selected plan variant for every recipient.
|
||||
func Merge(documents [][]byte) ([]byte, error) {
|
||||
out := fpdf.New("P", "mm", "A4", "")
|
||||
out.SetMargins(0, 0, 0)
|
||||
out.SetAutoPageBreak(false, 0)
|
||||
|
||||
for _, raw := range documents {
|
||||
if len(raw) == 0 {
|
||||
continue
|
||||
}
|
||||
var reader io.ReadSeeker = bytes.NewReader(raw)
|
||||
importer := gofpdi.NewImporter()
|
||||
template := importer.ImportPageFromStream(out, &reader, 1, "/MediaBox")
|
||||
sizes := importer.GetPageSizes()
|
||||
pages := len(sizes)
|
||||
if pages == 0 {
|
||||
return nil, fmt.Errorf("PDF ohne Seiten")
|
||||
}
|
||||
for page := 1; page <= pages; page++ {
|
||||
size, ok := sizes[page]["/MediaBox"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("PDF-Seite %d ohne MediaBox", page)
|
||||
}
|
||||
width, height := size["w"], size["h"]
|
||||
orientation := "P"
|
||||
if width > height {
|
||||
orientation = "L"
|
||||
}
|
||||
out.AddPageFormat(orientation, fpdf.SizeType{Wd: width, Ht: height})
|
||||
if page > 1 {
|
||||
template = importer.ImportPageFromStream(out, &reader, page, "/MediaBox")
|
||||
}
|
||||
importer.UseImportedTemplate(out, template, 0, 0, width, height)
|
||||
}
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
if err := out.Output(&buffer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := out.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
Reference in new issue
Block a user