Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Add landscape and page size support to weaver #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions weaver/converter/athenapdf/athenapdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,30 @@ type AthenaPDF struct {
// an '-A' command-line flag to indicate aggressive content extraction
// (ideal for a clutter-free reading experience).
Aggressive bool
// NoPortrait will set the output PDF to be in landscape instead of in
// portrait orientation
NoPortrait bool
// Sets the page size for the PDF
PageSize string
}

// constructCMD returns a string array containing the AthenaPDF command to be
// executed by Go's os/exec Output. It does this using a base command, and path
// string.
// It will set an additional '-A' flag if aggressive is set to true.
// See athenapdf CLI for more information regarding the aggressive mode.
func constructCMD(base string, path string, aggressive bool) []string {
func constructCMD(base string, path string, aggressive bool, noPortrait bool, pageSize string) []string {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also IMHO this method should receive the AthenaPDF instance and take the params from there. Not sure why the struct was split out to separate params.

args := strings.Fields(base)
args = append(args, path)
if aggressive {
args = append(args, "-A")
}
if noPortrait {
args = append(args, "--no-portrait")
}
if len(pageSize) > 0 {
args = append(args, "-P", pageSize);
}
return args
}

Expand All @@ -45,7 +56,7 @@ func (c AthenaPDF) Convert(s converter.ConversionSource, done <-chan struct{}) (
log.Printf("[AthenaPDF] converting to PDF: %s\n", s.GetActualURI())

// Construct the command to execute
cmd := constructCMD(c.CMD, s.URI, c.Aggressive)
cmd := constructCMD(c.CMD, s.URI, c.Aggressive, c.NoPortrait, c.PageSize)
out, err := gcmd.Execute(cmd, done)
if err != nil {
return nil, err
Expand Down
19 changes: 17 additions & 2 deletions weaver/converter/athenapdf/athenapdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,35 @@ import (
)

func TestConstructCMD(t *testing.T) {
got := constructCMD("athenapdf -S -T 120", "test_file.html", false)
got := constructCMD("athenapdf -S -T 120", "test_file.html", false, false, "")
want := []string{"athenapdf", "-S", "-T", "120", "test_file.html"}
if !reflect.DeepEqual(got, want) {
t.Errorf("expected constructed athenapdf command to be %+v, got %+v", want, got)
}
}

func TestConstructCMD_aggressive(t *testing.T) {
cmd := constructCMD("athenapdf -S -T 60", "test_file.html", true)
cmd := constructCMD("athenapdf -S -T 60", "test_file.html", true, false, "")
if got, want := cmd[len(cmd)-1], "-A"; got != want {
t.Errorf("expected last argument of constructed athenapdf command to be %s, got %+v", want, got)
}
}

func TestConstructCMD_landscape(t *testing.T) {
cmd := constructCMD("athenapdf -S -T 60", "test_file.html", false, true, "")
if got, want := cmd[len(cmd)-1], "--no-portrait"; got != want {
t.Errorf("expected last argument of constructed athenapdf command to be %s, got %+v", want, got)
}
}

func TestConstructCMD_pageSizeA3(t *testing.T) {
got := constructCMD("athenapdf -S -T 60", "test_file.html", false, false, "A3")
want := []string{"athenapdf", "-S", "-T", "60", "test_file.html", "-P", "A3"}
if !reflect.DeepEqual(got, want) {
t.Errorf("expected constructed athenapdf command to be %+v, got %+v", want, got)
}
}

func mockConversion(path string, tmp bool, cmd string) ([]byte, error) {
c := AthenaPDF{}
c.CMD = cmd
Expand Down
4 changes: 3 additions & 1 deletion weaver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func conversionHandler(c *gin.Context, source converter.ConversionSource) {
}

_, aggressive := c.GetQuery("aggressive")
_, noPortrait := c.GetQuery("no_portrait")
pageSize := c.Query("page_size")

conf := c.MustGet("config").(Config)
wq := c.MustGet("queue").(chan<- converter.Work)
Expand All @@ -73,7 +75,7 @@ func conversionHandler(c *gin.Context, source converter.ConversionSource) {
uploadConversion := converter.UploadConversion{baseConversion, awsConf}

StartConversion:
conversion = athenapdf.AthenaPDF{uploadConversion, conf.AthenaCMD, aggressive}
conversion = athenapdf.AthenaPDF{uploadConversion, conf.AthenaCMD, aggressive, noPortrait, pageSize}
if attempts != 0 {
cc := cloudconvert.Client{
conf.CloudConvert.APIUrl,
Expand Down