mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
feat(api): accept A0, A1 and A2 as paper sizes
This commit is contained in:
@@ -405,9 +405,16 @@ func JSON[T any](form *FormData, key string, target *T, want string) *FormData {
|
||||
})
|
||||
}
|
||||
|
||||
// PaperSizeNames lists the paper size names [FormData.PaperSize] and
|
||||
// [PaperSizeInches] accept, as error messages write them.
|
||||
const PaperSizeNames = "A0, A1, A2, A3, A4, A5, B4, B5, Letter, Legal or Tabloid"
|
||||
|
||||
// paperSizes maps the paper size names to their width and height in
|
||||
// millimeters, portrait.
|
||||
var paperSizes = map[string][2]float64{
|
||||
"a0": {841, 1189},
|
||||
"a1": {594, 841},
|
||||
"a2": {420, 594},
|
||||
"a3": {297, 420},
|
||||
"a4": {210, 297},
|
||||
"a5": {148, 210},
|
||||
@@ -419,9 +426,9 @@ var paperSizes = map[string][2]float64{
|
||||
}
|
||||
|
||||
// PaperSize binds the width and height, in inches, of the paper size a form
|
||||
// field names: A3, A4, A5, B4, B5, Letter, Legal or Tabloid, in any case. It
|
||||
// leaves the targets untouched when the field is empty, and populates an
|
||||
// error for an unknown name.
|
||||
// field names: one of [PaperSizeNames], in any case. It leaves the targets
|
||||
// untouched when the field is empty, and populates an error for an unknown
|
||||
// name.
|
||||
//
|
||||
// var width, height float64
|
||||
//
|
||||
@@ -434,7 +441,7 @@ func (form *FormData) PaperSize(key string, width, height *float64) *FormData {
|
||||
|
||||
w, h, ok := PaperSizeInches(value)
|
||||
if !ok {
|
||||
form.append(&invalidFieldError{key: key, value: value, err: errors.New("want A3, A4, A5, B4, B5, Letter, Legal or Tabloid")})
|
||||
form.append(&invalidFieldError{key: key, value: value, err: errors.New("want " + PaperSizeNames)})
|
||||
return form
|
||||
}
|
||||
|
||||
@@ -445,8 +452,8 @@ func (form *FormData) PaperSize(key string, width, height *float64) *FormData {
|
||||
}
|
||||
|
||||
// PaperSizeInches returns the width and height, in inches, portrait, of the
|
||||
// paper size name: A3, A4, A5, B4, B5, Letter, Legal or Tabloid, in any case.
|
||||
// ok is false for any other name.
|
||||
// paper size name: one of [PaperSizeNames], in any case. ok is false for any
|
||||
// other name.
|
||||
func PaperSizeInches(name string) (width, height float64, ok bool) {
|
||||
size, ok := paperSizes[strings.ToLower(name)]
|
||||
if !ok {
|
||||
|
||||
@@ -1949,7 +1949,8 @@ func TestFormData_PaperSize(t *testing.T) {
|
||||
{value: "A4", wantWidth: 210 / 25.4, wantHeight: 297 / 25.4},
|
||||
{value: "letter", wantWidth: 8.5, wantHeight: 11},
|
||||
{value: "Tabloid", wantWidth: 11, wantHeight: 17},
|
||||
{value: "A0", wantWidth: -1, wantHeight: -1, wantErr: true},
|
||||
{value: "a0", wantWidth: 841 / 25.4, wantHeight: 1189 / 25.4},
|
||||
{value: "A7", wantWidth: -1, wantHeight: -1, wantErr: true},
|
||||
} {
|
||||
t.Run(tc.value, func(t *testing.T) {
|
||||
form := &FormData{values: map[string][]string{"paperSize": {tc.value}}}
|
||||
@@ -1976,7 +1977,10 @@ func TestPaperSizeInches(t *testing.T) {
|
||||
{name: "A4", wantWidth: 210 / 25.4, wantHeight: 297 / 25.4, wantOk: true},
|
||||
{name: "letter", wantWidth: 8.5, wantHeight: 11, wantOk: true},
|
||||
{name: "LEGAL", wantWidth: 8.5, wantHeight: 14, wantOk: true},
|
||||
{name: "A0"},
|
||||
{name: "A0", wantWidth: 841 / 25.4, wantHeight: 1189 / 25.4, wantOk: true},
|
||||
{name: "A1", wantWidth: 594 / 25.4, wantHeight: 841 / 25.4, wantOk: true},
|
||||
{name: "a2", wantWidth: 420 / 25.4, wantHeight: 594 / 25.4, wantOk: true},
|
||||
{name: "A7"},
|
||||
{name: ""},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
@@ -278,7 +278,7 @@ func (s *composeStage) validate() error {
|
||||
if s.PaperSize != "" {
|
||||
width, height, ok := api.PaperSizeInches(s.PaperSize)
|
||||
if !ok {
|
||||
return fmt.Errorf(`"paperSize" must be A3, A4, A5, B4, B5, Letter, Legal or Tabloid, got %q`, s.PaperSize)
|
||||
return fmt.Errorf(`"paperSize" must be %s, got %q`, api.PaperSizeNames, s.PaperSize)
|
||||
}
|
||||
|
||||
s.width, s.height = width, height
|
||||
|
||||
@@ -287,7 +287,7 @@ func TestComposeStage(t *testing.T) {
|
||||
{name: "no size", raw: `[{"op":"compose",` + placements + `}]`, wantErr: `stage 0: compose: set "paperSize", or "paperWidth" and "paperHeight"`},
|
||||
{name: "a width alone", raw: `[{"op":"compose","paperWidth":"6in",` + placements + `}]`, wantErr: `compose: set "paperSize", or "paperWidth" and "paperHeight"`},
|
||||
{name: "a height alone", raw: `[{"op":"compose","paperHeight":"5in",` + placements + `}]`, wantErr: `compose: set "paperSize", or "paperWidth" and "paperHeight"`},
|
||||
{name: "unknown paper size", raw: `[{"op":"compose","paperSize":"A0",` + placements + `}]`, wantErr: `compose: "paperSize" must be A3, A4, A5, B4, B5, Letter, Legal or Tabloid, got "A0"`},
|
||||
{name: "unknown paper size", raw: `[{"op":"compose","paperSize":"A7",` + placements + `}]`, wantErr: `compose: "paperSize" must be A0, A1, A2, A3, A4, A5, B4, B5, Letter, Legal or Tabloid, got "A7"`},
|
||||
{name: "width not a length", raw: `[{"op":"compose","paperWidth":"wide","paperHeight":"5in",` + placements + `}]`, wantErr: `compose: "paperWidth" must be a length such as "6in", "15cm" or "432pt", got "wide"`},
|
||||
{name: "height not a length", raw: `[{"op":"compose","paperWidth":"6in","paperHeight":"5ft",` + placements + `}]`, wantErr: `compose: "paperHeight" must be a length such as "6in", "15cm" or "432pt", got "5ft"`},
|
||||
{name: "zero width", raw: `[{"op":"compose","paperWidth":"0in","paperHeight":"5in",` + placements + `}]`, wantErr: `compose: "paperWidth" must be greater than 0, got "0in"`},
|
||||
|
||||
@@ -426,12 +426,12 @@ Feature: /forms/libreoffice/convert
|
||||
"""
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| files | testdata/page_1.docx | file |
|
||||
| paperSize | A0 | field |
|
||||
| paperSize | A7 | field |
|
||||
Then the response status code should be 400
|
||||
Then the response header "Content-Type" should be "text/plain; charset=UTF-8"
|
||||
Then the response body should match string:
|
||||
"""
|
||||
Invalid form data: form field 'paperSize' is invalid (got 'A0', want A3, A4, A5, B4, B5, Letter, Legal or Tabloid)
|
||||
Invalid form data: form field 'paperSize' is invalid (got 'A7', want A0, A1, A2, A3, A4, A5, B4, B5, Letter, Legal or Tabloid)
|
||||
"""
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| files | testdata/page_1.docx | file |
|
||||
|
||||
Reference in New Issue
Block a user