Compare commits

...

3 Commits
6.1.2 ... 6.2.0

Author SHA1 Message Date
Julien Neuhart
1ff16cc54b minor refactoring: using default scale from default options 2020-02-10 10:10:49 +01:00
Julien Neuhart
3609cf54c0 Merge pull request #168 from mdjward/feature/chrome-scaling-support
Add support for Chrome scaling.
2020-02-10 10:03:24 +01:00
Matt Ward
198038f5ea Add support for Chrome scaling. 2020-02-01 19:10:40 +00:00
7 changed files with 95 additions and 10 deletions

View File

@@ -24,7 +24,7 @@ For instance:
```bash ```bash
$ git clone https://github.com/thecodingmachine/gotenberg.git $ git clone https://github.com/thecodingmachine/gotenberg.git
$ make publish GOTENBERG_USER_GID=your_custom_gid GOTENBERG_USER_UID=your_custom_uid DOCKER_REGISTRY=your_registry DOCKER_USER=registry_user DOCKER_PASSWORD=registry_password VERSION=6.1.0 $ make publish GOTENBERG_USER_GID=your_custom_gid GOTENBERG_USER_UID=your_custom_uid DOCKER_REGISTRY=your_registry DOCKER_USER=registry_user DOCKER_PASSWORD=registry_password VERSION=6.2.0
``` ```
> `master` branch is always up-to-date with the latest version of the API. > `master` branch is always up-to-date with the latest version of the API.

View File

@@ -243,11 +243,11 @@ $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```
## Paper size, margins, orientation ## Paper size, margins, orientation, scaling
You may also customize the resulting PDF format. You may also customize the resulting PDF format.
By default, it will be rendered with `A4` size, `1 inch` margins and `portrait` orientation. By default, it will be rendered with `A4` size, `1 inch` margins and `portrait` orientation and 100% (`1.0`) page scale.
> Paper size and margins have to be provided in `inches`. Same for margins. > Paper size and margins have to be provided in `inches`. Same for margins.
@@ -265,6 +265,7 @@ $ curl --request POST \
--form marginLeft=0 \ --form marginLeft=0 \
--form marginRight=0 \ --form marginRight=0 \
--form landscape=true \ --form landscape=true \
--form scale=0.75 \
-o result.pdf -o result.pdf
``` ```
@@ -279,6 +280,7 @@ req := gotenberg.NewHTMLRequest(index)
req.PaperSize(gotenberg.A4) req.PaperSize(gotenberg.A4)
req.Margins(gotenberg.NoMargins) req.Margins(gotenberg.NoMargins)
req.Landscape(true) req.Landscape(true)
req.Scale(0.75)
dest := "result.pdf" dest := "result.pdf"
c.Store(req, dest) c.Store(req, dest)
``` ```
@@ -297,6 +299,7 @@ $request = new HTMLRequest($index);
$request->setPaperSize(Request::A4); $request->setPaperSize(Request::A4);
$request->setMargins(Request::NO_MARGINS); $request->setMargins(Request::NO_MARGINS);
$request->setLandscape(true); $request->setLandscape(true);
$request->setScale(0.75);
$dest = 'result.pdf'; $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
``` ```

View File

@@ -675,19 +675,19 @@ $dest = 'result.pdf';
$client->store($request, $dest); $client->store($request, $dest);
</pre> </pre>
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation" href="#html.paper_size_margins_orientation"> <h2 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation_scaling" href="#html.paper_size_margins_orientation_scaling">
<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> <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>Paper size, margins, orientation</h2> </a>Paper size, margins, orientation, scaling</h2>
<p>You may also customize the resulting PDF format.</p> <p>You may also customize the resulting PDF format.</p>
<p>By default, it will be rendered with <code>A4</code> size, <code>1 inch</code> margins and <code>portrait</code> orientation.</p> <p>By default, it will be rendered with <code>A4</code> size, <code>1 inch</code> margins and <code>portrait</code> orientation and 100% (<code>1.0</code>) page scale.</p>
<blockquote> <blockquote>
<p>Paper size and margins have to be provided in <code>inches</code>. Same for margins.</p> <p>Paper size and margins have to be provided in <code>inches</code>. Same for margins.</p>
</blockquote> </blockquote>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation.c_url" href="#html.paper_size_margins_orientation.c_url"> <h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation_scaling.c_url" href="#html.paper_size_margins_orientation_scaling.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> <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> </a>cURL</h3>
@@ -702,10 +702,11 @@ $client-&gt;store($request, $dest);
</span><span class="se"></span> --form <span class="nv">marginLeft</span><span class="o">=</span><span class="m">0</span> <span class="se">\ </span><span class="se"></span> --form <span class="nv">marginLeft</span><span class="o">=</span><span class="m">0</span> <span class="se">\
</span><span class="se"></span> --form <span class="nv">marginRight</span><span class="o">=</span><span class="m">0</span> <span class="se">\ </span><span class="se"></span> --form <span class="nv">marginRight</span><span class="o">=</span><span class="m">0</span> <span class="se">\
</span><span class="se"></span> --form <span class="nv">landscape</span><span class="o">=</span><span class="nb">true</span> <span class="se">\ </span><span class="se"></span> --form <span class="nv">landscape</span><span class="o">=</span><span class="nb">true</span> <span class="se">\
</span><span class="se"></span> --form <span class="nv">scale</span><span class="o">=</span>0.75 <span class="se">\
</span><span class="se"></span> -o result.pdf </span><span class="se"></span> -o result.pdf
</pre> </pre>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation.go" href="#html.paper_size_margins_orientation.go"> <h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation_scaling.go" href="#html.paper_size_margins_orientation_scaling.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> <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> </a>Go</h3>
@@ -717,11 +718,12 @@ $client-&gt;store($request, $dest);
<span class="nx">req</span><span class="p">.</span><span class="nf">PaperSize</span><span class="p">(</span><span class="nx">gotenberg</span><span class="p">.</span><span class="nx">A4</span><span class="p">)</span> <span class="nx">req</span><span class="p">.</span><span class="nf">PaperSize</span><span class="p">(</span><span class="nx">gotenberg</span><span class="p">.</span><span class="nx">A4</span><span class="p">)</span>
<span class="nx">req</span><span class="p">.</span><span class="nf">Margins</span><span class="p">(</span><span class="nx">gotenberg</span><span class="p">.</span><span class="nx">NoMargins</span><span class="p">)</span> <span class="nx">req</span><span class="p">.</span><span class="nf">Margins</span><span class="p">(</span><span class="nx">gotenberg</span><span class="p">.</span><span class="nx">NoMargins</span><span class="p">)</span>
<span class="nx">req</span><span class="p">.</span><span class="nf">Landscape</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span> <span class="nx">req</span><span class="p">.</span><span class="nf">Landscape</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
<span class="nx">req</span><span class="p">.</span><span class="nf">Scale</span><span class="p">(</span><span class="mf">0.75</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">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> <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> </pre>
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation.php" href="#html.paper_size_margins_orientation.php"> <h3 class="Heading"><a class="Anchor" aria-hidden="true" id="html.paper_size_margins_orientation_scaling.php" href="#html.paper_size_margins_orientation_scaling.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> <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> </a>PHP</h3>
@@ -736,6 +738,7 @@ $request = new HTMLRequest($index);
$request-&gt;setPaperSize(Request::A4); $request-&gt;setPaperSize(Request::A4);
$request-&gt;setMargins(Request::NO_MARGINS); $request-&gt;setMargins(Request::NO_MARGINS);
$request-&gt;setLandscape(true); $request-&gt;setLandscape(true);
$request-&gt;setScale(0.75);
$dest = &#39;result.pdf&#39;; $dest = &#39;result.pdf&#39;;
$client-&gt;store($request, $dest); $client-&gt;store($request, $dest);
</pre> </pre>

View File

@@ -56,6 +56,10 @@ func chromePrinterOptions(r resource.Resource, config conf.Config) (printer.Chro
if err != nil { if err != nil {
return printer.ChromePrinterOptions{}, err return printer.ChromePrinterOptions{}, err
} }
scale, err := resource.ScaleArg(r, config)
if err != nil {
return printer.ChromePrinterOptions{}, err
}
return printer.ChromePrinterOptions{ return printer.ChromePrinterOptions{
WaitTimeout: waitTimeout, WaitTimeout: waitTimeout,
WaitDelay: waitDelay, WaitDelay: waitDelay,
@@ -71,6 +75,7 @@ func chromePrinterOptions(r resource.Resource, config conf.Config) (printer.Chro
PageRanges: pageRanges, PageRanges: pageRanges,
RpccBufferSize: googleChromeRpccBufferSize, RpccBufferSize: googleChromeRpccBufferSize,
CustomHTTPHeaders: make(map[string]string), CustomHTTPHeaders: make(map[string]string),
Scale: scale,
}, nil }, nil
} }
opts, err := resolver() opts, err := resolver()

View File

@@ -57,6 +57,9 @@ const (
// GoogleChromeRpccBufferSizeArgKey is the key // GoogleChromeRpccBufferSizeArgKey is the key
// of the argument "googleChromeRpccBufferSize". // of the argument "googleChromeRpccBufferSize".
GoogleChromeRpccBufferSizeArgKey ArgKey = "googleChromeRpccBufferSize" GoogleChromeRpccBufferSizeArgKey ArgKey = "googleChromeRpccBufferSize"
// ScaleArgKey is the key
// of the argument "scale".
ScaleArgKey ArgKey = "scale"
) )
/* /*
@@ -81,6 +84,7 @@ func ArgKeys() []ArgKey {
LandscapeArgKey, LandscapeArgKey,
PageRangesArgKey, PageRangesArgKey,
GoogleChromeRpccBufferSizeArgKey, GoogleChromeRpccBufferSizeArgKey,
ScaleArgKey,
} }
} }
@@ -294,3 +298,22 @@ func GoogleChromeRpccBufferSizeArg(r Resource, config conf.Config) (int64, error
} }
return result, nil return result, nil
} }
/*
ScaleArg is a helper for retrieving
the "scale" argument as float64.
*/
func ScaleArg(r Resource, config conf.Config) (float64, error) {
const op string = "resource.ScaleArg"
opts := printer.DefaultChromePrinterOptions(config)
result, err := r.Float64Arg(
ScaleArgKey,
opts.Scale,
xassert.Float64NotInferiorTo(0.1),
xassert.Float64NotSuperiorTo(2.0),
)
if err != nil {
return result, xerror.New(op, err)
}
return result, nil
}

View File

@@ -26,6 +26,7 @@ func TestArgKeys(t *testing.T) {
LandscapeArgKey, LandscapeArgKey,
PageRangesArgKey, PageRangesArgKey,
GoogleChromeRpccBufferSizeArgKey, GoogleChromeRpccBufferSizeArgKey,
ScaleArgKey,
} }
assert.Equal(t, expected, ArgKeys()) assert.Equal(t, expected, ArgKeys())
} }
@@ -346,3 +347,50 @@ func TestGoogleChromeRpccBufferSizeArg(t *testing.T) {
err = r.Close() err = r.Close()
assert.Nil(t, err) assert.Nil(t, err)
} }
func TestScaleArg(t *testing.T) {
const (
resourceDirectoryName string = "foo"
defaultValue float64 = 1.0
)
var expected float64
logger := test.DebugLogger()
config := conf.DefaultConfig()
r, err := New(logger, resourceDirectoryName)
assert.Nil(t, err)
// argument does not exist.
expected = defaultValue
v, err := ScaleArg(r, config)
assert.Nil(t, err)
assert.Equal(t, expected, v)
// argument exist.
expected = 0.5
r.WithArg(ScaleArgKey, "0.5")
v, err = ScaleArg(r, config)
assert.Nil(t, err)
assert.Equal(t, expected, v)
// should not be OK as argument
// value is < 0.1.
expected = defaultValue
r.WithArg(ScaleArgKey, "-1.0")
v, err = ScaleArg(r, config)
test.AssertError(t, err)
assert.Equal(t, expected, v)
// should not be OK as argument
// value is > 2.0.
expected = defaultValue
r.WithArg(ScaleArgKey, "2.1")
v, err = ScaleArg(r, config)
test.AssertError(t, err)
assert.Equal(t, expected, v)
// should not be OK as
// argument value is invalid.
expected = defaultValue
r.WithArg(ScaleArgKey, "foo")
v, err = ScaleArg(r, config)
test.AssertError(t, err)
assert.Equal(t, expected, v)
// finally...
err = r.Close()
assert.Nil(t, err)
}

View File

@@ -45,6 +45,7 @@ type ChromePrinterOptions struct {
PageRanges string PageRanges string
RpccBufferSize int64 RpccBufferSize int64
CustomHTTPHeaders map[string]string CustomHTTPHeaders map[string]string
Scale float64
} }
// DefaultChromePrinterOptions returns the default // DefaultChromePrinterOptions returns the default
@@ -66,6 +67,7 @@ func DefaultChromePrinterOptions(config conf.Config) ChromePrinterOptions {
PageRanges: "", PageRanges: "",
RpccBufferSize: config.DefaultGoogleChromeRpccBufferSize(), RpccBufferSize: config.DefaultGoogleChromeRpccBufferSize(),
CustomHTTPHeaders: make(map[string]string), CustomHTTPHeaders: make(map[string]string),
Scale: 1.0,
} }
} }
@@ -176,7 +178,8 @@ func (p chromePrinter) Print(destination string) error {
SetDisplayHeaderFooter(true). SetDisplayHeaderFooter(true).
SetHeaderTemplate(p.opts.HeaderHTML). SetHeaderTemplate(p.opts.HeaderHTML).
SetFooterTemplate(p.opts.FooterHTML). SetFooterTemplate(p.opts.FooterHTML).
SetPrintBackground(true) SetPrintBackground(true).
SetScale(p.opts.Scale)
if p.opts.PageRanges != "" { if p.opts.PageRanges != "" {
printToPdfArgs.SetPageRanges(p.opts.PageRanges) printToPdfArgs.SetPageRanges(p.opts.PageRanges)
} }