Merge pull request #122 from thecodingmachine/page_ranges

pageRanges formField
This commit is contained in:
Julien Neuhart
2019-12-10 15:38:01 +01:00
committed by GitHub
12 changed files with 365 additions and 28 deletions

View File

@@ -301,6 +301,53 @@ $dest = 'result.pdf';
$client->store($request, $dest);
```
## Page ranges
You may specify the page ranges to convert.
The format is the same as the one from the print options
of Google Chrome, e.g. `1-5,8,11-13`.
### cURL
```bash
$ curl --request POST \
--url http://localhost:3000/convert/html \
--header 'Content-Type: multipart/form-data' \
--form files=@index.html \
--form pageRanges='1-3,5' \
-o result.pdf
```
### Go
```golang
import "github.com/thecodingmachine/gotenberg-go-client/v7"
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
index, _ := gotenberg.NewDocumentFromPath("index.html", "/path/to/file")
req := gotenberg.NewHTMLRequest(index)
req.PageRanges("1-3,5")
dest := "result.pdf"
c.Store(req, dest)
```
### PHP
```php
use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
use TheCodingMachine\Gotenberg\Request;
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
$request = new HTMLRequest($index);
$request->setPageRanges('1-3,5');
$dest = 'result.pdf';
$client->store($request, $dest);
```
## Wait delay
In some cases, you may want to wait a certain amount of time to make sure the

View File

@@ -114,3 +114,54 @@ $request->setLandscape(true);
$dest = 'result.pdf';
$client->store($request, $dest);
```
## Page ranges
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:** if more than one document, the page ranges will be
> applied for each document.
### cURL
```bash
$ curl --request POST \
--url http://localhost:3000/convert/office \
--header 'Content-Type: multipart/form-data' \
--form files=@document.docx \
--form pageRanges='1-3' \
-o result.pdf
```
### Go
```golang
import "github.com/thecodingmachine/gotenberg-go-client/v6"
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
doc, _ := gotenberg.NewDocumentFromPath("document.docx", "/path/to/file")
req := gotenberg.NewOfficeRequest(doc)
req.PageRanges("1-3")
dest := "result.pdf"
c.Store(req, dest)
```
### PHP
```php
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', '/path/to/file'),
];
$request = new OfficeRequest($files);
$request->setPageRanges('1-3');
$dest = 'result.pdf';
$client->store($request, $dest);
```

View File

@@ -740,6 +740,58 @@ $dest = 'result.pdf';
$client->store($request, $dest);
</pre>
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="html.page_ranges" href="#html.page_ranges">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Page ranges</h2>
<p>You may specify the page ranges to convert.</p>
<p>The format is the same as the one from the print options
of Google Chrome, e.g. <code>1-5,8,11-13</code>.</p>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.page_ranges.c_url" href="#html.page_ranges.c_url">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>cURL</h3>
<pre class="chroma">$ curl --request POST <span class="se">\
</span><span class="se"></span> --url http://localhost:3000/convert/html <span class="se">\
</span><span class="se"></span> --header <span class="s1">&#39;Content-Type: multipart/form-data&#39;</span> <span class="se">\
</span><span class="se"></span> --form <span class="nv">files</span><span class="o">=</span>@index.html <span class="se">\
</span><span class="se"></span> --form <span class="nv">pageRanges</span><span class="o">=</span><span class="s1">&#39;1-3,5&#39;</span> <span class="se">\
</span><span class="se"></span> -o result.pdf
</pre>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.page_ranges.go" href="#html.page_ranges.go">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Go</h3>
<pre class="chroma"><span class="kn">import</span> <span class="s">&#34;github.com/thecodingmachine/gotenberg-go-client/v7&#34;</span>
<span class="nx">c</span> <span class="o">:=</span> <span class="o">&amp;</span><span class="nx">gotenberg</span><span class="p">.</span><span class="nx">Client</span><span class="p">{</span><span class="nx">Hostname</span><span class="p">:</span> <span class="s">&#34;http://localhost:3000&#34;</span><span class="p">}</span>
<span class="nx">index</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">gotenberg</span><span class="p">.</span><span class="nf">NewDocumentFromPath</span><span class="p">(</span><span class="s">&#34;index.html&#34;</span><span class="p">,</span> <span class="s">&#34;/path/to/file&#34;</span><span class="p">)</span>
<span class="nx">req</span> <span class="o">:=</span> <span class="nx">gotenberg</span><span class="p">.</span><span class="nf">NewHTMLRequest</span><span class="p">(</span><span class="nx">index</span><span class="p">)</span>
<span class="nx">req</span><span class="p">.</span><span class="nf">PageRanges</span><span class="p">(</span><span class="s">&#34;1-3,5&#34;</span><span class="p">)</span>
<span class="nx">dest</span> <span class="o">:=</span> <span class="s">&#34;result.pdf&#34;</span>
<span class="nx">c</span><span class="p">.</span><span class="nf">Store</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">dest</span><span class="p">)</span>
</pre>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.page_ranges.php" href="#html.page_ranges.php">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>PHP</h3>
<pre class="chroma">use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
use TheCodingMachine\Gotenberg\Request;
$client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client());
$index = DocumentFactory::makeFromPath(&#39;index.html&#39;, &#39;/path/to/file&#39;);
$request = new HTMLRequest($index);
$request-&gt;setPageRanges(&#39;1-3,5&#39;);
$dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest);
</pre>
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="html.wait_delay" href="#html.wait_delay">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Wait delay</h2>
@@ -1181,6 +1233,64 @@ $request = new OfficeRequest($files);
$request-&gt;setLandscape(true);
$dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest);
</pre>
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="office.page_ranges" href="#office.page_ranges">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Page ranges</h2>
<p>You may specify the page ranges to convert.</p>
<p>The format is the same as the one from the print options
of LibreOffice, e.g. <code>1-1</code> or <code>1-4</code>.</p>
<blockquote>
<p><strong>Attention:</strong> if more than one document, the page ranges will be
applied for each document.</p>
</blockquote>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.page_ranges.c_url" href="#office.page_ranges.c_url">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>cURL</h3>
<pre class="chroma">$ curl --request POST <span class="se">\
</span><span class="se"></span> --url http://localhost:3000/convert/office <span class="se">\
</span><span class="se"></span> --header <span class="s1">&#39;Content-Type: multipart/form-data&#39;</span> <span class="se">\
</span><span class="se"></span> --form <span class="nv">files</span><span class="o">=</span>@document.docx <span class="se">\
</span><span class="se"></span> --form <span class="nv">pageRanges</span><span class="o">=</span><span class="s1">&#39;1-3&#39;</span> <span class="se">\
</span><span class="se"></span> -o result.pdf
</pre>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.page_ranges.go" href="#office.page_ranges.go">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>Go</h3>
<pre class="chroma"><span class="kn">import</span> <span class="s">&#34;github.com/thecodingmachine/gotenberg-go-client/v6&#34;</span>
<span class="nx">c</span> <span class="o">:=</span> <span class="o">&amp;</span><span class="nx">gotenberg</span><span class="p">.</span><span class="nx">Client</span><span class="p">{</span><span class="nx">Hostname</span><span class="p">:</span> <span class="s">&#34;http://localhost:3000&#34;</span><span class="p">}</span>
<span class="nx">doc</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nx">gotenberg</span><span class="p">.</span><span class="nf">NewDocumentFromPath</span><span class="p">(</span><span class="s">&#34;document.docx&#34;</span><span class="p">,</span> <span class="s">&#34;/path/to/file&#34;</span><span class="p">)</span>
<span class="nx">req</span> <span class="o">:=</span> <span class="nx">gotenberg</span><span class="p">.</span><span class="nf">NewOfficeRequest</span><span class="p">(</span><span class="nx">doc</span><span class="p">)</span>
<span class="nx">req</span><span class="p">.</span><span class="nf">PageRanges</span><span class="p">(</span><span class="s">&#34;1-3&#34;</span><span class="p">)</span>
<span class="nx">dest</span> <span class="o">:=</span> <span class="s">&#34;result.pdf&#34;</span>
<span class="nx">c</span><span class="p">.</span><span class="nf">Store</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">dest</span><span class="p">)</span>
</pre>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.page_ranges.php" href="#office.page_ranges.php">
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-link"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg>
</a>PHP</h3>
<pre class="chroma">use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\OfficeRequest;
$client = new Client(&#39;http://localhost:3000&#39;, new \Http\Adapter\Guzzle6\Client());
$files = [
DocumentFactory::makeFromPath(&#39;document.docx&#39;, &#39;/path/to/file&#39;),
];
$request = new OfficeRequest($files);
$request-&gt;setPageRanges(&#39;1-3&#39;);
$dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest);
</pre>
</div>

View File

@@ -48,23 +48,29 @@ func chromePrinterOptions(r resource.Resource, config conf.Config) (printer.Chro
if err != nil {
return printer.ChromePrinterOptions{}, err
}
pageRanges, err := r.StringArg(resource.PageRangesArgKey, "")
if err != nil {
return printer.ChromePrinterOptions{}, err
}
googleChromeRpccBufferSize, err := resource.GoogleChromeRpccBufferSizeArg(r, config)
if err != nil {
return printer.ChromePrinterOptions{}, err
}
return printer.ChromePrinterOptions{
WaitTimeout: waitTimeout,
WaitDelay: waitDelay,
HeaderHTML: headerHTML,
FooterHTML: footerHTML,
PaperWidth: paperWidth,
PaperHeight: paperHeight,
MarginTop: marginTop,
MarginBottom: marginBottom,
MarginLeft: marginLeft,
MarginRight: marginRight,
Landscape: landscape,
RpccBufferSize: googleChromeRpccBufferSize,
WaitTimeout: waitTimeout,
WaitDelay: waitDelay,
HeaderHTML: headerHTML,
FooterHTML: footerHTML,
PaperWidth: paperWidth,
PaperHeight: paperHeight,
MarginTop: marginTop,
MarginBottom: marginBottom,
MarginLeft: marginLeft,
MarginRight: marginRight,
Landscape: landscape,
PageRanges: pageRanges,
RpccBufferSize: googleChromeRpccBufferSize,
CustomHTTPHeaders: make(map[string]string),
}, nil
}
opts, err := resolver()
@@ -85,9 +91,14 @@ func officePrinterOptions(r resource.Resource, config conf.Config) (printer.Offi
if err != nil {
return printer.OfficePrinterOptions{}, err
}
pageRanges, err := r.StringArg(resource.PageRangesArgKey, "")
if err != nil {
return printer.OfficePrinterOptions{}, err
}
return printer.OfficePrinterOptions{
WaitTimeout: waitTimeout,
Landscape: landscape,
PageRanges: pageRanges,
}, nil
}
opts, err := resolver()

View File

@@ -51,6 +51,9 @@ const (
// LandscapeArgKey is the key
// of the argument "landscape".
LandscapeArgKey ArgKey = "landscape"
// PageRangesArgKey is the key
// of the argument "pageRanges".
PageRangesArgKey ArgKey = "pageRanges"
// GoogleChromeRpccBufferSizeArgKey is the key
// of the argument "googleChromeRpccBufferSize".
GoogleChromeRpccBufferSizeArgKey ArgKey = "googleChromeRpccBufferSize"
@@ -76,6 +79,7 @@ func ArgKeys() []ArgKey {
MarginLeftArgKey,
MarginRightArgKey,
LandscapeArgKey,
PageRangesArgKey,
GoogleChromeRpccBufferSizeArgKey,
}
}

View File

@@ -24,6 +24,7 @@ func TestArgKeys(t *testing.T) {
MarginLeftArgKey,
MarginRightArgKey,
LandscapeArgKey,
PageRangesArgKey,
GoogleChromeRpccBufferSizeArgKey,
}
assert.Equal(t, expected, ArgKeys())

View File

@@ -42,6 +42,7 @@ type ChromePrinterOptions struct {
MarginLeft float64
MarginRight float64
Landscape bool
PageRanges string
RpccBufferSize int64
CustomHTTPHeaders map[string]string
}
@@ -62,6 +63,7 @@ func DefaultChromePrinterOptions(config conf.Config) ChromePrinterOptions {
MarginLeft: 1.0,
MarginRight: 1.0,
Landscape: false,
PageRanges: "",
RpccBufferSize: config.DefaultGoogleChromeRpccBufferSize(),
CustomHTTPHeaders: make(map[string]string),
}
@@ -163,23 +165,35 @@ func (p chromePrinter) Print(destination string) error {
} else {
p.logger.DebugOp(op, "no wait delay to apply, moving on...")
}
printToPdfArgs := page.NewPrintToPDFArgs().
SetPaperWidth(p.opts.PaperWidth).
SetPaperHeight(p.opts.PaperHeight).
SetMarginTop(p.opts.MarginTop).
SetMarginBottom(p.opts.MarginBottom).
SetMarginLeft(p.opts.MarginLeft).
SetMarginRight(p.opts.MarginRight).
SetLandscape(p.opts.Landscape).
SetDisplayHeaderFooter(true).
SetHeaderTemplate(p.opts.HeaderHTML).
SetFooterTemplate(p.opts.FooterHTML).
SetPrintBackground(true)
if p.opts.PageRanges != "" {
printToPdfArgs.SetPageRanges(p.opts.PageRanges)
}
// print the page to PDF.
print, err := targetClient.Page.PrintToPDF(
ctx,
page.NewPrintToPDFArgs().
SetPaperWidth(p.opts.PaperWidth).
SetPaperHeight(p.opts.PaperHeight).
SetMarginTop(p.opts.MarginTop).
SetMarginBottom(p.opts.MarginBottom).
SetMarginLeft(p.opts.MarginLeft).
SetMarginRight(p.opts.MarginRight).
SetLandscape(p.opts.Landscape).
SetDisplayHeaderFooter(true).
SetHeaderTemplate(p.opts.HeaderHTML).
SetFooterTemplate(p.opts.FooterHTML).
SetPrintBackground(true),
printToPdfArgs,
)
if err != nil {
// 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,
)
}
if strings.Contains(err.Error(), "rpcc: message too large") {
return xerror.Invalid(
op,

View File

@@ -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)

View File

@@ -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)

View File

@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/phayes/freeport"
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
@@ -27,6 +28,7 @@ type officePrinter struct {
type OfficePrinterOptions struct {
WaitTimeout float64
Landscape bool
PageRanges string
}
// DefaultOfficePrinterOptions returns the default
@@ -35,6 +37,7 @@ func DefaultOfficePrinterOptions(config conf.Config) OfficePrinterOptions {
return OfficePrinterOptions{
WaitTimeout: config.DefaultWaitTimeout(),
Landscape: false,
PageRanges: "",
}
}
@@ -62,7 +65,7 @@ func (p officePrinter) Print(destination string) error {
baseFilename := xrand.Get()
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
p.logger.DebugfOp(op, "converting '%s' to PDF...", fpath)
if err := unoconv(ctx, p.logger, fpath, tmpDest, p.opts); err != nil {
if err := p.unoconv(ctx, fpath, tmpDest); err != nil {
return err
}
p.logger.DebugfOp(op, "'%s.pdf' created", baseFilename)
@@ -88,7 +91,7 @@ func (p officePrinter) Print(destination string) error {
return nil
}
func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string, opts OfficePrinterOptions) error {
func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error {
const op string = "printer.unoconv"
resolver := func() error {
port, err := freeport.GetFreePort()
@@ -103,11 +106,25 @@ func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string,
"--format",
"pdf",
}
if opts.Landscape {
if p.opts.Landscape {
args = append(args, "--printer", "PaperOrientation=landscape")
}
if p.opts.PageRanges != "" {
args = append(args, "--export", fmt.Sprintf("PageRange=%s", p.opts.PageRanges))
}
args = append(args, "--output", destination, fpath)
return xexec.Run(ctx, logger, "unoconv", args...)
if err := xexec.Run(ctx, p.logger, "unoconv", args...); err != nil {
// 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,
)
}
return err
}
return nil
}
if err := resolver(); err != nil {
return xerror.New(op, err)

View File

@@ -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)

View File

@@ -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)