mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
feat(libreoffice): add a locale form field
This commit is contained in:
@@ -12,7 +12,7 @@ ARG GOLANG_VERSION=1.27.3
|
||||
FROM debian:13-slim AS lok-binary-stage
|
||||
|
||||
# See https://github.com/gotenberg/lok/releases.
|
||||
ARG LOK_VERSION=v0.0.6
|
||||
ARG LOK_VERSION=v0.0.7
|
||||
|
||||
RUN apt-get update -qq \
|
||||
&& DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends ca-certificates curl \
|
||||
@@ -192,13 +192,6 @@ COPY --link --from=gotenberg-binary-stage /home/gotenberg /usr/bin/
|
||||
# Copy lok.
|
||||
COPY --link --from=lok-binary-stage /home/lok /usr/bin/lok
|
||||
|
||||
# Keep the system locale empty, as soffice leaves it. LibreOfficeKit sets it
|
||||
# to en-US at startup, which makes Letter the fallback page size of documents
|
||||
# that set none, such as spreadsheets, CSV and TXT files. Empty, the image's
|
||||
# paper size applies: A4, as in Gotenberg 8.
|
||||
# See https://github.com/gotenberg/gotenberg/issues/1723.
|
||||
COPY --link build/libreoffice-system-locale.xcd /usr/lib/libreoffice/share/registry/gotenberg-system-locale.xcd
|
||||
|
||||
# Copy dictionnaries so that hyphens work on Chromium.
|
||||
# See https://github.com/gotenberg/gotenberg/issues/1293.
|
||||
COPY --link --chown="$GOTENBERG_USER_UID:$GOTENBERG_USER_GID" build/chromium-hyphen-data /opt/gotenberg/chromium-hyphen-data
|
||||
@@ -329,13 +322,6 @@ COPY --link --from=gotenberg-binary-stage /home/gotenberg-libreoffice /usr/bin/g
|
||||
# Copy lok.
|
||||
COPY --link --from=lok-binary-stage /home/lok /usr/bin/lok
|
||||
|
||||
# Keep the system locale empty, as soffice leaves it. LibreOfficeKit sets it
|
||||
# to en-US at startup, which makes Letter the fallback page size of documents
|
||||
# that set none, such as spreadsheets, CSV and TXT files. Empty, the image's
|
||||
# paper size applies: A4, as in Gotenberg 8.
|
||||
# See https://github.com/gotenberg/gotenberg/issues/1723.
|
||||
COPY --link build/libreoffice-system-locale.xcd /usr/lib/libreoffice/share/registry/gotenberg-system-locale.xcd
|
||||
|
||||
ENV LIBREOFFICE_BIN_PATH=/usr/lib/libreoffice/program/soffice.bin
|
||||
ENV LOK_BIN_PATH=/usr/bin/lok
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<oor:data xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oor="http://openoffice.org/2001/registry">
|
||||
<oor:component-data oor:package="org.openoffice" oor:name="Setup">
|
||||
<node oor:name="L10N">
|
||||
<prop oor:name="ooSetupSystemLocale" oor:finalized="true"><value></value></prop>
|
||||
</node>
|
||||
</oor:component-data>
|
||||
</oor:data>
|
||||
@@ -106,6 +106,12 @@ type Options struct {
|
||||
// Password specifies the password for opening the source file.
|
||||
Password string // #nosec
|
||||
|
||||
// Locale is a BCP 47 language tag, such as en-AU, that formats the
|
||||
// document's locale-dependent content: built-in date, number and currency
|
||||
// formats, date fields, and the fallback paper size of a document that
|
||||
// sets none. Empty follows the system locale.
|
||||
Locale string
|
||||
|
||||
// Landscape allows changing the orientation of the resulting PDF.
|
||||
Landscape bool
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ type lokRequest struct {
|
||||
InputPath string `json:"inputPath"`
|
||||
OutputPath string `json:"outputPath"`
|
||||
Password string `json:"password,omitempty"` //nolint:gosec // document open password, sent to the local lok process only
|
||||
Locale string `json:"locale,omitempty"`
|
||||
// EnableMacros is always sent, so lok's own default never applies. See
|
||||
// --libreoffice-enable-macros.
|
||||
EnableMacros bool `json:"enableMacros"`
|
||||
@@ -194,6 +195,7 @@ func newLokRequest(inputPath, outputPath string, options Options) (lokRequest, e
|
||||
InputPath: inputPath,
|
||||
OutputPath: outputPath,
|
||||
Password: options.Password,
|
||||
Locale: options.Locale,
|
||||
Landscape: options.Landscape,
|
||||
PaperFormat: paperFormat,
|
||||
PaperWidth: paperWidth,
|
||||
|
||||
@@ -190,6 +190,32 @@ func TestNewLokRequest_Paper(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The request sends the locale only when the client sets one, so that lok
|
||||
// keeps the system locale otherwise.
|
||||
func TestNewLokRequest_Locale(t *testing.T) {
|
||||
options := DefaultOptions()
|
||||
|
||||
req, err := newLokRequest("in.xlsx", "out.pdf", options)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal request: %v", err)
|
||||
}
|
||||
if strings.Contains(string(b), `"locale"`) {
|
||||
t.Fatalf("expected no locale, got %s", b)
|
||||
}
|
||||
|
||||
options.Locale = "en-AU"
|
||||
|
||||
req, err = newLokRequest("in.xlsx", "out.pdf", options)
|
||||
if err != nil || req.Locale != "en-AU" {
|
||||
t.Fatalf("en-AU: %+v, %v, want locale en-AU", req, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lok runs macros when enableMacros is true, so the request must say false
|
||||
// rather than rely on lok's default.
|
||||
func TestNewLokRequest_DisablesMacrosExplicitly(t *testing.T) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -49,6 +50,21 @@ func parseRotation(value string) (int, error) {
|
||||
return int(math.Round(math.Mod(degrees, 360) * 10)), nil
|
||||
}
|
||||
|
||||
// localePattern matches the BCP 47 language tags lok accepts: a language, an
|
||||
// optional script and an optional region, such as fr, en-AU, es-419 or
|
||||
// zh-Hans-CN. lok passes the tag to LibreOffice among comma-separated load
|
||||
// options, so nothing else may get through.
|
||||
var localePattern = regexp.MustCompile(`^[A-Za-z]{2,3}(-[A-Za-z]{4})?(-(?:[A-Za-z]{2}|[0-9]{3}))?$`)
|
||||
|
||||
// parseLocale checks a BCP 47 language tag.
|
||||
func parseLocale(value string) (string, error) {
|
||||
if !localePattern.MatchString(value) {
|
||||
return "", errors.New("want a BCP 47 language tag, such as en-AU or de-DE")
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// enum maps the names of a form field's values to the numbers LibreOffice
|
||||
// reads, in the order of those numbers.
|
||||
type enum []string
|
||||
@@ -81,7 +97,7 @@ const pdfuaProfile = "PDF/UA-1"
|
||||
|
||||
// into returns a [api.FormData.Custom] binding that parses a value into
|
||||
// target, and keeps target's default when the field is empty.
|
||||
func into(target *int, parse func(string) (int, error)) func(string) error {
|
||||
func into[T any](target *T, parse func(string) (T, error)) func(string) error {
|
||||
return func(value string) error {
|
||||
if value == "" {
|
||||
return nil
|
||||
|
||||
@@ -46,6 +46,7 @@ func convertRoute(libreOffice libreofficeapi.Converter, processor *pdf.Processor
|
||||
err = form.
|
||||
MandatoryPaths(libreOffice.Extensions(), &inputPaths).
|
||||
String("password", &options.Password).
|
||||
Custom("locale", into(&options.Locale, parseLocale)).
|
||||
Bool("landscape", &options.Landscape).
|
||||
Inches("paperWidth", &options.PaperWidth).
|
||||
Inches("paperHeight", &options.PaperHeight).
|
||||
|
||||
@@ -548,3 +548,55 @@ func TestConvertRoute_UnknownFields(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestConvertRoute_Locale checks that a language tag reaches LibreOffice, and
|
||||
// that anything else, such as a value that would add a load option, answers
|
||||
// 400 before LibreOffice runs.
|
||||
func TestConvertRoute_Locale(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
value string
|
||||
want string
|
||||
}{
|
||||
{"", ""},
|
||||
{"en-AU", "en-AU"},
|
||||
{"zh-Hans-CN", "zh-Hans-CN"},
|
||||
{"en_AU", "400"},
|
||||
{"en-AU,Batch=false", "400"},
|
||||
} {
|
||||
t.Run(tc.value, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
ctx := &api.ContextMock{Context: &api.Context{Context: context.Background()}}
|
||||
ctx.SetDirPath(dir)
|
||||
ctx.SetFiles(map[string]string{"page_1.docx": zipPackage(t, dir, "page_1.docx")})
|
||||
ctx.SetValues(map[string][]string{"locale": {tc.value}})
|
||||
ctx.SetLogger(slog.New(slog.DiscardHandler))
|
||||
|
||||
got := "400"
|
||||
converter := &libreofficeapi.ApiMock{
|
||||
ExtensionsMock: func() []string { return []string{".docx"} },
|
||||
PdfMock: func(_ context.Context, _ *slog.Logger, _, _ string, options libreofficeapi.Options) error {
|
||||
got = options.Locale
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
c := echo.New().NewContext(httptest.NewRequest(http.MethodPost, "/forms/libreoffice/convert", nil), httptest.NewRecorder())
|
||||
c.Set("context", ctx.Context)
|
||||
|
||||
err := convertRoute(converter, new(pdf.Processor)).Handler(c)
|
||||
if tc.want == "400" {
|
||||
status, message := api.ParseError(err)
|
||||
if status != http.StatusBadRequest || !strings.Contains(message, "'locale'") {
|
||||
t.Fatalf("got %d %q, want a 400 naming 'locale'", status, message)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if got != tc.want {
|
||||
t.Fatalf("LibreOffice got locale %q, want %q (err %v)", got, tc.want, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,6 +268,63 @@ Feature: /forms/libreoffice/convert
|
||||
Then there should be 1 PDF(s) in the response
|
||||
Then the "foo.pdf" PDF first page should be 288x432 points
|
||||
|
||||
# The locale formats Excel's locale-dependent formats, here a short date, a
|
||||
# system long date, a number and a currency, and picks the fallback paper
|
||||
# size. A conversion without one follows the system locale, whatever an
|
||||
# earlier conversion set: LibreOfficeKit keeps the language of a load.
|
||||
# See https://github.com/gotenberg/gotenberg/issues/1737.
|
||||
Scenario: POST /forms/libreoffice/convert (Locale)
|
||||
Given I have a default Gotenberg container
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| files | testdata/locale.xlsx | file |
|
||||
| locale | de-DE | field |
|
||||
| Gotenberg-Output-Filename | foo | header |
|
||||
Then the response status code should be 200
|
||||
Then there should be 1 PDF(s) in the response
|
||||
Then the "foo.pdf" PDF first page should be 595.3x841.9 points
|
||||
Then the "foo.pdf" PDF should have the following content at page 1:
|
||||
"""
|
||||
08.01.2026
|
||||
"""
|
||||
Then the "foo.pdf" PDF should have the following content at page 1:
|
||||
"""
|
||||
Donnerstag, 8. Januar 2026
|
||||
"""
|
||||
Then the "foo.pdf" PDF should have the following content at page 1:
|
||||
"""
|
||||
1.234.567,89 €
|
||||
"""
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| files | testdata/locale.xlsx | file |
|
||||
| locale | en-US | field |
|
||||
| Gotenberg-Output-Filename | foo | header |
|
||||
Then the response status code should be 200
|
||||
Then there should be 1 PDF(s) in the response
|
||||
Then the "foo.pdf" PDF first page should be 612x792 points
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| files | testdata/locale.xlsx | file |
|
||||
| locale | de-DE | field |
|
||||
| Gotenberg-Output-Filename | foo | header |
|
||||
Then the response status code should be 200
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| files | testdata/locale.xlsx | file |
|
||||
| Gotenberg-Output-Filename | foo | header |
|
||||
Then the response status code should be 200
|
||||
Then there should be 1 PDF(s) in the response
|
||||
Then the "foo.pdf" PDF first page should be 595.3x841.9 points
|
||||
Then the "foo.pdf" PDF should have the following content at page 1:
|
||||
"""
|
||||
1/8/2026
|
||||
"""
|
||||
Then the "foo.pdf" PDF should have the following content at page 1:
|
||||
"""
|
||||
Thursday, January 8, 2026
|
||||
"""
|
||||
Then the "foo.pdf" PDF should have the following content at page 1:
|
||||
"""
|
||||
$1,234,567.89
|
||||
"""
|
||||
|
||||
# A field the route does not read answers 400, and a Gotenberg 8 one says what
|
||||
# replaced it, a file field included.
|
||||
Scenario: POST /forms/libreoffice/convert (Unknown Form Fields)
|
||||
@@ -293,47 +350,49 @@ Feature: /forms/libreoffice/convert
|
||||
Scenario: POST /forms/libreoffice/convert (Bad Request)
|
||||
Given I have a default Gotenberg container
|
||||
When I make a "POST" request to Gotenberg at the "/forms/libreoffice/convert" endpoint with the following form data and header(s):
|
||||
| landscape | foo | field |
|
||||
| exportFormFields | foo | field |
|
||||
| allowDuplicateFieldNames | foo | field |
|
||||
| nativeBookmarks | foo | field |
|
||||
| exportBookmarksToPdfDestination | foo | field |
|
||||
| exportPlaceholders | foo | field |
|
||||
| exportNotes | foo | field |
|
||||
| exportNotesPages | foo | field |
|
||||
| exportOnlyNotesPages | foo | field |
|
||||
| exportNotesInMargin | foo | field |
|
||||
| convertOooTargetToPdfTarget | foo | field |
|
||||
| exportLinksRelativeFsys | foo | field |
|
||||
| exportHiddenSlides | foo | field |
|
||||
| skipEmptyPages | foo | field |
|
||||
| addOriginalDocumentAsStream | foo | field |
|
||||
| singlePage | foo | field |
|
||||
| nativeTaggedPdf | foo | field |
|
||||
| initialView | foo | field |
|
||||
| initialPage | -1 | field |
|
||||
| magnification | foo | field |
|
||||
| zoom | -1 | field |
|
||||
| pageLayout | foo | field |
|
||||
| resizeWindowToInitialPage | foo | field |
|
||||
| centerWindow | foo | field |
|
||||
| openInFullScreenMode | foo | field |
|
||||
| displayPDFDocumentTitle | foo | field |
|
||||
| hideViewerMenubar | foo | field |
|
||||
| hideViewerToolbar | foo | field |
|
||||
| hideViewerWindowControls | foo | field |
|
||||
| useTransitionEffects | foo | field |
|
||||
| openBookmarkLevels | 15 | field |
|
||||
| nativeLosslessImageCompression | foo | field |
|
||||
| nativeQuality | -1 | field |
|
||||
| nativeReduceImageResolution | foo | field |
|
||||
| nativeMaxImageResolution | 10 | field |
|
||||
| locale | en_AU | field |
|
||||
| landscape | foo | field |
|
||||
| exportFormFields | foo | field |
|
||||
| allowDuplicateFieldNames | foo | field |
|
||||
| nativeBookmarks | foo | field |
|
||||
| exportBookmarksToPdfDestination | foo | field |
|
||||
| exportPlaceholders | foo | field |
|
||||
| exportNotes | foo | field |
|
||||
| exportNotesPages | foo | field |
|
||||
| exportOnlyNotesPages | foo | field |
|
||||
| exportNotesInMargin | foo | field |
|
||||
| convertOooTargetToPdfTarget | foo | field |
|
||||
| exportLinksRelativeFsys | foo | field |
|
||||
| exportHiddenSlides | foo | field |
|
||||
| skipEmptyPages | foo | field |
|
||||
| addOriginalDocumentAsStream | foo | field |
|
||||
| singlePage | foo | field |
|
||||
| nativeTaggedPdf | foo | field |
|
||||
| initialView | foo | field |
|
||||
| initialPage | -1 | field |
|
||||
| magnification | foo | field |
|
||||
| zoom | -1 | field |
|
||||
| pageLayout | foo | field |
|
||||
| resizeWindowToInitialPage | foo | field |
|
||||
| centerWindow | foo | field |
|
||||
| openInFullScreenMode | foo | field |
|
||||
| displayPDFDocumentTitle | foo | field |
|
||||
| hideViewerMenubar | foo | field |
|
||||
| hideViewerToolbar | foo | field |
|
||||
| hideViewerWindowControls | foo | field |
|
||||
| useTransitionEffects | foo | field |
|
||||
| openBookmarkLevels | 15 | field |
|
||||
| nativeLosslessImageCompression | foo | field |
|
||||
| nativeQuality | -1 | field |
|
||||
| nativeReduceImageResolution | foo | field |
|
||||
| nativeMaxImageResolution | 10 | 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 header "Gotenberg-Version" should be "{version}"
|
||||
Then the response body should match string:
|
||||
"""
|
||||
Invalid form data: no form file found for extensions: [.123 .602 .abw .bib .bmp .cdr .cgm .cmx .csv .cwk .dbf .dif .doc .docm .docx .dot .dotm .dotx .dxf .emf .eps .fodg .fodp .fods .fodt .fopd .gif .htm .html .hwp .jpeg .jpg .key .ltx .lwp .mcw .met .mml .mw .numbers .odd .odg .odm .odp .ods .odt .otg .oth .otp .ots .ott .pages .pbm .pcd .pct .pcx .pdb .pdf .pgm .png .pot .potm .potx .ppm .pps .ppsm .ppsx .ppt .pptm .pptx .psd .psw .pub .pwp .pxl .ras .rtf .sda .sdc .sdd .sdp .sdw .sgl .slk .smf .stc .std .sti .stw .svg .svm .swf .sxc .sxd .sxg .sxi .sxm .sxw .tga .tif .tiff .txt .uof .uop .uos .uot .vdx .vor .vsd .vsdm .vsdx .wb2 .wk1 .wks .wmf .wpd .wpg .wps .xbm .xhtml .xls .xlsb .xlsm .xlsx .xlt .xltm .xltx .xlw .xml .xpm .zabw]
|
||||
form field 'locale' is invalid (got 'en_AU', want a BCP 47 language tag, such as en-AU or de-DE)
|
||||
form field 'landscape' is invalid (got 'foo', want true or false)
|
||||
form field 'exportFormFields' is invalid (got 'foo', want true or false)
|
||||
form field 'allowDuplicateFieldNames' is invalid (got 'foo', want true or false)
|
||||
|
||||
BIN
test/integration/testdata/locale.xlsx
vendored
Normal file
BIN
test/integration/testdata/locale.xlsx
vendored
Normal file
Binary file not shown.
Reference in New Issue
Block a user