use TheCodingMachine\Gotenberg\Client;
+use TheCodingMachine\Gotenberg\DocumentFactory;
+use TheCodingMachine\Gotenberg\OfficeRequest;
+
+$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
+$files = [
+ DocumentFactory::makeFromPath('document.docx', 'document.docx'),
+];
+$request = new OfficeRequest($files);
+$request->setPageRanges("1-3");
+$dest = "result.pdf";
+$client->store($request, $dest);
From 827060b6cec5c5a290b88222b71cf8f65ff477f6 Mon Sep 17 00:00:00 2001
From: Julien Neuhart
Date: Fri, 4 Oct 2019 18:22:46 +0200
Subject: [PATCH 3/6] emphazing that pageRanges does not work if more than one
office document
---
build/docs/content/07-office.md | 2 +-
docs/index.html | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/build/docs/content/07-office.md b/build/docs/content/07-office.md
index 74745587..5cded130 100644
--- a/build/docs/content/07-office.md
+++ b/build/docs/content/07-office.md
@@ -123,7 +123,7 @@ You may specify the page ranges to convert.
The format is the same as the one from the print options
of LibreOffice, e.g. `1-1` or `1-4`.
-> This feature does not work in there is more
+> **Attention:** this feature does not work in there is more
> than one document to convert.
### cURL
diff --git a/docs/index.html b/docs/index.html
index cbf92684..fbc2a471 100755
--- a/docs/index.html
+++ b/docs/index.html
@@ -1066,7 +1066,7 @@ $client->store($request, $dest);
of LibreOffice, e.g. 1-1 or 1-4.
-
This feature does not work in there is more
+
Attention: this feature does not work in there is more
than one document to convert.
From ca0784fa594f7883400a47edd5dc0ecef118a26f Mon Sep 17 00:00:00 2001
From: Julien Neuhart
Date: Sat, 5 Oct 2019 12:09:39 +0200
Subject: [PATCH 4/6] adding tests for page ranges
---
internal/app/xhttp/pkg/resource/arg_test.go | 1 +
internal/pkg/printer/chrome.go | 4 ++--
internal/pkg/printer/html_test.go | 20 +++++++++++++++++++
internal/pkg/printer/markdown_test.go | 22 +++++++++++++++++++++
internal/pkg/printer/office.go | 10 ++++------
internal/pkg/printer/office_test.go | 20 +++++++++++++++++++
internal/pkg/printer/url_test.go | 20 +++++++++++++++++++
7 files changed, 89 insertions(+), 8 deletions(-)
diff --git a/internal/app/xhttp/pkg/resource/arg_test.go b/internal/app/xhttp/pkg/resource/arg_test.go
index 3c7f6b02..f42fd1d0 100644
--- a/internal/app/xhttp/pkg/resource/arg_test.go
+++ b/internal/app/xhttp/pkg/resource/arg_test.go
@@ -24,6 +24,7 @@ func TestArgKeys(t *testing.T) {
MarginLeftArgKey,
MarginRightArgKey,
LandscapeArgKey,
+ PageRangesArgKey,
}
assert.Equal(t, expected, ArgKeys())
}
diff --git a/internal/pkg/printer/chrome.go b/internal/pkg/printer/chrome.go
index f7cca718..41b4c99d 100644
--- a/internal/pkg/printer/chrome.go
+++ b/internal/pkg/printer/chrome.go
@@ -161,15 +161,15 @@ func (p chromePrinter) Print(destination string) error {
printToPdfArgs.SetPageRanges(p.opts.PageRanges)
}
// print the page to PDF.
- // TODO catch page range error?
print, err := targetClient.Page.PrintToPDF(
ctx,
printToPdfArgs,
)
if err != nil {
+ // TODO: find a way to check it in the handlers.
if strings.Contains(err.Error(), "Page range syntax error") {
return xerror.Invalid(
- "",
+ op,
fmt.Sprintf("'%s' is not a valid Google Chrome page ranges", p.opts.PageRanges),
err,
)
diff --git a/internal/pkg/printer/html_test.go b/internal/pkg/printer/html_test.go
index 4a233163..e69539fa 100644
--- a/internal/pkg/printer/html_test.go
+++ b/internal/pkg/printer/html_test.go
@@ -38,6 +38,26 @@ func TestHTMLPrinter(t *testing.T) {
assert.Nil(t, err)
err = os.RemoveAll(dest)
assert.Nil(t, err)
+ // options with a page ranges.
+ opts = DefaultChromePrinterOptions(config)
+ opts.PageRanges = "1"
+ p = NewHTMLPrinter(logger, fpath, opts)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ assert.Nil(t, err)
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
+ // should not be OK as options have
+ // a wrong page ranges.
+ opts = DefaultChromePrinterOptions(config)
+ opts.PageRanges = "foo"
+ p = NewHTMLPrinter(logger, fpath, opts)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ test.AssertError(t, err)
+ assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
// should not be OK as context.Context
// should timeout.
opts = DefaultChromePrinterOptions(config)
diff --git a/internal/pkg/printer/markdown_test.go b/internal/pkg/printer/markdown_test.go
index c5115a3a..9627cdea 100644
--- a/internal/pkg/printer/markdown_test.go
+++ b/internal/pkg/printer/markdown_test.go
@@ -40,6 +40,28 @@ func TestMarkdownPrinter(t *testing.T) {
assert.Nil(t, err)
err = os.RemoveAll(dest)
assert.Nil(t, err)
+ // options with a page ranges.
+ opts = DefaultChromePrinterOptions(config)
+ opts.PageRanges = "1"
+ p, err = NewMarkdownPrinter(logger, fpath, opts)
+ assert.Nil(t, err)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ assert.Nil(t, err)
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
+ // should not be OK as options have
+ // a wrong page ranges.
+ opts = DefaultChromePrinterOptions(config)
+ opts.PageRanges = "foo"
+ p, err = NewMarkdownPrinter(logger, fpath, opts)
+ assert.Nil(t, err)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ test.AssertError(t, err)
+ assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
// should not be OK as context.Context
// should timeout.
opts = DefaultChromePrinterOptions(config)
diff --git a/internal/pkg/printer/office.go b/internal/pkg/printer/office.go
index 7112acda..e0f4f20b 100644
--- a/internal/pkg/printer/office.go
+++ b/internal/pkg/printer/office.go
@@ -91,9 +91,6 @@ func (p officePrinter) Print(destination string) error {
func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error {
const op string = "printer.unoconv"
resolver := func() error {
- hasPageRanges := func() bool {
- return p.opts.PageRanges != "" && len(p.fpaths) == 1
- }
port, err := freeport.GetFreePort()
if err != nil {
return err
@@ -109,14 +106,15 @@ func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) e
if p.opts.Landscape {
args = append(args, "--printer", "PaperOrientation=landscape")
}
- if hasPageRanges() {
+ if p.opts.PageRanges != "" {
args = append(args, "--export", fmt.Sprintf("PageRange=%s", p.opts.PageRanges))
}
args = append(args, "--output", destination, fpath)
if err := xexec.Run(ctx, p.logger, "unoconv", args...); err != nil {
- if hasPageRanges() && strings.Contains(err.Error(), "exit status 5") {
+ // TODO: find a way to check it in the handlers.
+ if p.opts.PageRanges != "" && strings.Contains(err.Error(), "exit status 5") {
return xerror.Invalid(
- "",
+ op,
fmt.Sprintf("'%s' is not a valid LibreOffice page ranges", p.opts.PageRanges),
err,
)
diff --git a/internal/pkg/printer/office_test.go b/internal/pkg/printer/office_test.go
index b6a0235b..5eb8992e 100644
--- a/internal/pkg/printer/office_test.go
+++ b/internal/pkg/printer/office_test.go
@@ -46,6 +46,26 @@ func TestOfficePrinter(t *testing.T) {
assert.Nil(t, err)
err = os.RemoveAll(dest)
assert.Nil(t, err)
+ // options with page ranges.
+ opts = DefaultOfficePrinterOptions(config)
+ opts.PageRanges = "1-1"
+ p = NewOfficePrinter(logger, []string{fpaths[0]}, opts)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ assert.Nil(t, err)
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
+ // should not be OK as options have
+ // a wrong page ranges.
+ opts = DefaultOfficePrinterOptions(config)
+ opts.PageRanges = "foo"
+ p = NewOfficePrinter(logger, []string{fpaths[0]}, opts)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ test.AssertError(t, err)
+ assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
// should not be OK as context.Context
// should timeout.
opts = DefaultOfficePrinterOptions(config)
diff --git a/internal/pkg/printer/url_test.go b/internal/pkg/printer/url_test.go
index 2c0b99f7..ef477e75 100644
--- a/internal/pkg/printer/url_test.go
+++ b/internal/pkg/printer/url_test.go
@@ -38,6 +38,26 @@ func TestURLPrinter(t *testing.T) {
assert.Nil(t, err)
err = os.RemoveAll(dest)
assert.Nil(t, err)
+ // options with a pages ranges.
+ opts = DefaultChromePrinterOptions(config)
+ opts.PageRanges = "1"
+ p = NewURLPrinter(logger, URL, opts)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ assert.Nil(t, err)
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
+ // should not be OK as options have
+ // a wrong page ranges.
+ opts = DefaultChromePrinterOptions(config)
+ opts.PageRanges = "foo"
+ p = NewURLPrinter(logger, URL, opts)
+ dest = test.GenerateDestination()
+ err = p.Print(dest)
+ test.AssertError(t, err)
+ assert.Equal(t, xerror.InvalidCode, xerror.Code(err))
+ err = os.RemoveAll(dest)
+ assert.Nil(t, err)
// should not be OK as context.Context
// should timeout.
opts = DefaultChromePrinterOptions(config)
From 3255f8af0c1f0efe6da9e545233b044dac3d9580 Mon Sep 17 00:00:00 2001
From: Julien Neuhart
Date: Sat, 5 Oct 2019 12:13:06 +0200
Subject: [PATCH 5/6] updating documentation: if office and more than one
document, page ranges will be applied for each of them
---
build/docs/content/07-office.md | 4 ++--
docs/index.html | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/build/docs/content/07-office.md b/build/docs/content/07-office.md
index 5cded130..546ee7de 100644
--- a/build/docs/content/07-office.md
+++ b/build/docs/content/07-office.md
@@ -123,8 +123,8 @@ You may specify the page ranges to convert.
The format is the same as the one from the print options
of LibreOffice, e.g. `1-1` or `1-4`.
-> **Attention:** this feature does not work in there is more
-> than one document to convert.
+> **Attention:** if more than one document, the page ranges will be
+> applied for each document.
### cURL
diff --git a/docs/index.html b/docs/index.html
index fbc2a471..b03e62e3 100755
--- a/docs/index.html
+++ b/docs/index.html
@@ -1066,8 +1066,8 @@ $client->store($request, $dest);
of LibreOffice, e.g. 1-1 or 1-4.
-
Attention: this feature does not work in there is more
-than one document to convert.
+
Attention: if more than one document, the page ranges will be
+applied for each document.