Skip to content

Commit

Permalink
Merge pull request #207 from vsimakhin/refactor-pdf-lib
Browse files Browse the repository at this point in the history
refactor pdf export lib
  • Loading branch information
vsimakhin authored Mar 27, 2024
2 parents 66ff740 + 1c0e6b2 commit 800e40d
Show file tree
Hide file tree
Showing 7 changed files with 908 additions and 900 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- Update: Refactored PDF export package. No UI changes.
- New: Add support for a custom title page for PDF A4/A5 exports.
- Update: Update openlayers lib from 7.3.0 to 9.0.0. No UI changes.
- Update: Update golang from 1.20.3 to 1.21.8. No UI changes.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You also can easily export all flight records into EASA style pdf format, print

## [Unreleased]

- Update: Refactored PDF export package. No UI changes.
- New: Add support for a custom title page for PDF A4/A5 exports.
- Update: Update openlayers lib from 7.3.0 to 9.0.0. No UI changes.
- Update: Update golang from 1.20.3 to 1.21.8. No UI changes.
Expand Down
84 changes: 45 additions & 39 deletions cmd/web/handlers_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"

Expand All @@ -27,7 +26,7 @@ func (app *application) HandlerExport(w http.ResponseWriter, r *http.Request) {
}
}

// HandlerExportLogbook executes the pdf export and returns pdf file
// HandlerExportLogbook serves the GET request for logbook export
func (app *application) HandlerExportLogbook(w http.ResponseWriter, r *http.Request) {
format := chi.URLParam(r, "format")

Expand All @@ -45,63 +44,70 @@ func (app *application) HandlerExportLogbook(w http.ResponseWriter, r *http.Requ
return
}

if format == exportA4 || format == exportA5 {
// export to PDF
var contentType, fileName string
var exportFunc func() error

w.Header().Set("Content-Type", "application/pdf")
w.Header().Set("Content-Disposition", "attachment; filename=logbook.pdf")
switch format {
case exportA4, exportA5:
contentType = "application/pdf"
fileName = "logbook.pdf"

logbook := &pdfexport.Logbook{
OwnerName: settings.OwnerName,
LicenseNumber: settings.LicenseNumber,
Address: settings.Address,
Signature: settings.SignatureText,
SignatureImage: settings.SignatureImage,
}
var customTitleUUID string
var exportSettings models.ExportPDF

if format == exportA4 {
logbook.Export = settings.ExportA4
customTitleUUID = settings.ExportA4.CustomTitle
exportSettings = settings.ExportA4
} else {
exportSettings = settings.ExportA5
}

} else if format == exportA5 {
logbook.Export = settings.ExportA5
customTitleUUID = settings.ExportA5.CustomTitle
if exportSettings.CustomTitle != "" {
att, _ := app.db.GetAttachmentByID(exportSettings.CustomTitle)
exportSettings.CustomTitleBlob = att.Document
}

att, _ := app.db.GetAttachmentByID(customTitleUUID)
logbook.Export.CustomTitleBlob = att.Document
pdfExporter, err := pdfexport.NewPDFExporter(format,
settings.OwnerName, settings.LicenseNumber, settings.Address,
settings.SignatureText, settings.SignatureImage, exportSettings)

err = logbook.ExportPDF(format, flightRecords, w)
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

} else if format == exportCSV {
// export to CSV format
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachment; filename=logbook.csv")
exportFunc = func() error {
if format == exportA4 {
return pdfExporter.ExportA4(flightRecords, w)
}
return pdfExporter.ExportA5(flightRecords, w)
}

case exportCSV:
contentType = "text/csv"
fileName = "logbook.csv"
var e csvexport.ExportCSV
e.ExportCSV = settings.ExportCSV
err = e.Export(flightRecords, w)

} else if format == exportXLS {
// export to XLS format

w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", "attachment; filename=logbook.xlsx")
exportFunc = func() error {
return e.Export(flightRecords, w)
}

case exportXLS:
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
fileName = "logbook.xlsx"
var xls xlsexport.ExportXLS
xls.ExportXLS = settings.ExportXLS

err = xls.Export(flightRecords, w)

} else {
err = errors.New("unknown export format")
exportFunc = func() error {
return xls.Export(flightRecords, w)
}
}

w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))

err = exportFunc()
if err != nil {
app.errorLog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

Expand Down
Loading

0 comments on commit 800e40d

Please sign in to comment.