Added ghostscript pdf compression and merging

This commit is contained in:
Dmitriy Shishkov 2023-11-28 23:20:58 +03:00
parent cd5b65a886
commit 98132b89b8
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
2 changed files with 31 additions and 11 deletions

View File

@ -6,18 +6,20 @@ import (
"time" "time"
) )
func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType) (resultingPath string, error error) { func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType) (resultingPath string, err error) {
switch contType { switch contType {
case Images: case Images:
return ImagesToPDF(folderPath, filesPaths) return ImagesToPDF(folderPath, filesPaths)
case Office: case Office:
return OfficeToPDF(folderPath, filesPaths) return OfficeToPDF(folderPath, filesPaths)
case Pdfs:
return PdfToPDF(folderPath, filesPaths)
default: default:
return "", fmt.Errorf("Unhandled ContentType with %d index", contType) return "", fmt.Errorf("unhandled ContentType with %d index", contType)
} }
} }
func ImagesToPDF(folderPath string, filesPaths []string) (resultingPath string, error error) { func ImagesToPDF(folderPath string, filesPaths []string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "Images") resultingPath = createResultingPath(folderPath, "Images")
if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil { if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil {
@ -27,10 +29,10 @@ func ImagesToPDF(folderPath string, filesPaths []string) (resultingPath string,
return resultingPath, nil return resultingPath, nil
} }
func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string, error error) { func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "Office document") resultingPath = createResultingPath(folderPath, "Office document")
err := runCommand( err = runCommand(
"soffice", "--headless", "--nologo", "--nofirststartwizard", "soffice", "--headless", "--nologo", "--nofirststartwizard",
"--norestore", "--convert-to", "pdf", "--outdir", folderPath, filesPaths[0], "--norestore", "--convert-to", "pdf", "--outdir", folderPath, filesPaths[0],
) )
@ -46,6 +48,21 @@ func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string,
return resultingPath, nil return resultingPath, nil
} }
func PdfToPDF(folderPath string, filesPath []string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "PDFs")
err = runCommand("gs", append([]string{
"-sDEVICE=pdfwrite", "-dCompressFonts=true", "-dPDFSETTINGS=/ebook",
"-dNOPAUSE", "-dQUIET", "-dBATCH", "-sOutputFile=" + resultingPath,
}, filesPath...)...)
if err != nil {
return "", err
}
return resultingPath, nil
}
func createResultingPath(folderPath string, suffix string) string { func createResultingPath(folderPath string, suffix string) string {
return folderPath + "/" + time.Now().Format(time.ANSIC) + " " + suffix + ".pdf" return folderPath + "/" + time.Now().Format(time.ANSIC) + " " + suffix + ".pdf"
} }

11
main.go
View File

@ -30,7 +30,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
const UPLOADS_FOLDER_PREFIX = "./uploads/" const UPLOADS_FOLDER_PREFIX = "./uploads/"
func saveFilesOnDisk(files []*multipart.FileHeader) (folderPath string, filesPaths []string, error error) { func saveFilesOnDisk(files []*multipart.FileHeader) (folderPath string, filesPaths []string, err error) {
// Create folder for uploaded files // Create folder for uploaded files
folderPath = UPLOADS_FOLDER_PREFIX + strconv.FormatInt(time.Now().Unix(), 10) folderPath = UPLOADS_FOLDER_PREFIX + strconv.FormatInt(time.Now().Unix(), 10)
@ -78,9 +78,10 @@ type ContentType int
const ( const (
Images ContentType = iota Images ContentType = iota
Office Office
Pdfs
) )
func detectContentType(files []*multipart.FileHeader) (contentType ContentType, error error) { func detectContentType(files []*multipart.FileHeader) (contentType ContentType, err error) {
for _, file := range files { for _, file := range files {
fileExt := strings.ToLower(filepath.Ext(file.Filename)) fileExt := strings.ToLower(filepath.Ext(file.Filename))
switch fileExt { switch fileExt {
@ -88,10 +89,12 @@ func detectContentType(files []*multipart.FileHeader) (contentType ContentType,
if len(files) == 1 { if len(files) == 1 {
return Office, nil return Office, nil
} else { } else {
return contentType, fmt.Errorf("Expected one document, got %d", len(files)) return contentType, fmt.Errorf("expected one document, got %d", len(files))
} }
case ".jpg", ".jpeg", ".png", ".pdf": case ".jpg", ".jpeg", ".png":
contentType = Images contentType = Images
case ".pdf":
contentType = Pdfs
default: default:
return contentType, fmt.Errorf("%s file extension is unsupported yet", fileExt) return contentType, fmt.Errorf("%s file extension is unsupported yet", fileExt)
} }