mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 20:52:14 +01:00
Merge pull request #41 from thecodingmachine/office-paperSize
removing paper size for Office conversion
This commit is contained in:
@@ -31,7 +31,7 @@ RUN curl -sL https://deb.nodesource.com/setup_9.x | bash - &&\
|
|||||||
|
|
||||||
COPY --from=hack /usr/bin/pm2 /usr/bin/pm2
|
COPY --from=hack /usr/bin/pm2 /usr/bin/pm2
|
||||||
COPY --from=hack /usr/share/pm2 /usr/share/pm2
|
COPY --from=hack /usr/share/pm2 /usr/share/pm2
|
||||||
COPY --from=hack /etc/default/pm2 /usr/default/pm2
|
COPY --from=hack /etc/default/pm2 /etc/default/pm2
|
||||||
COPY --from=hack /etc/systemd/system/pm2.service /etc/systemd/system/pm2.service
|
COPY --from=hack /etc/systemd/system/pm2.service /etc/systemd/system/pm2.service
|
||||||
|
|
||||||
# |--------------------------------------------------------------------------
|
# |--------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -69,14 +69,11 @@ $dirPath = "/foo";
|
|||||||
$filename = $client->store($request, $dirPath);
|
$filename = $client->store($request, $dirPath);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Paper size and orientation
|
## Orientation
|
||||||
|
|
||||||
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 and `portrait` orientation.
|
By default, it will be rendered with `portrait` orientation.
|
||||||
|
|
||||||
> Paper size has to be provided in `inches`.
|
|
||||||
> Also, you have to set both `paperWidth` and `paperHeight`.
|
|
||||||
|
|
||||||
### cURL
|
### cURL
|
||||||
|
|
||||||
@@ -85,8 +82,6 @@ $ curl --request POST \
|
|||||||
--url http://localhost:3000/convert/office \
|
--url http://localhost:3000/convert/office \
|
||||||
--header 'Content-Type: multipart/form-data' \
|
--header 'Content-Type: multipart/form-data' \
|
||||||
--form files=@document.docx \
|
--form files=@document.docx \
|
||||||
--form paperWidth=8.27 \
|
|
||||||
--form paperHeight=11.27 \
|
|
||||||
--form landscape=true \
|
--form landscape=true \
|
||||||
-o result.pdf
|
-o result.pdf
|
||||||
```
|
```
|
||||||
@@ -99,7 +94,6 @@ import "github.com/thecodingmachine/gotenberg/pkg"
|
|||||||
func main() {
|
func main() {
|
||||||
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
|
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
|
||||||
req, _ := gotenberg.NewOfficeRequest([]string{"document.docx"})
|
req, _ := gotenberg.NewOfficeRequest([]string{"document.docx"})
|
||||||
req.SetPaperSize(gotenberg.A4)
|
|
||||||
req.SetLandscape(true)
|
req.SetLandscape(true)
|
||||||
dest := "result.pdf"
|
dest := "result.pdf"
|
||||||
c.Store(req, dest)
|
c.Store(req, dest)
|
||||||
@@ -118,7 +112,6 @@ $files = [
|
|||||||
DocumentFactory::makeFromPath('document.docx', 'document.docx'),
|
DocumentFactory::makeFromPath('document.docx', 'document.docx'),
|
||||||
];
|
];
|
||||||
$request = new OfficeRequest($files);
|
$request = new OfficeRequest($files);
|
||||||
$request->setPaperSize(Request::A4);
|
|
||||||
$request->setLandscape(true);
|
$request->setLandscape(true);
|
||||||
$dirPath = "/foo";
|
$dirPath = "/foo";
|
||||||
$filename = $client->store($request, $dirPath);
|
$filename = $client->store($request, $dirPath);
|
||||||
|
|||||||
@@ -637,20 +637,15 @@ $dirPath = "/foo";
|
|||||||
$filename = $client->store($request, $dirPath);
|
$filename = $client->store($request, $dirPath);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="office.paper_size_and_orientation" href="#office.paper_size_and_orientation">
|
<h2 class="Heading"><a class="Anchor" aria-hidden="true" id="office.orientation" href="#office.orientation">
|
||||||
<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 and orientation</h2>
|
</a>Orientation</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 and <code>portrait</code> orientation.</p>
|
<p>By default, it will be rendered with <code>portrait</code> orientation.</p>
|
||||||
|
|
||||||
<blockquote>
|
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.orientation.c_url" href="#office.orientation.c_url">
|
||||||
<p>Paper size has to be provided in <code>inches</code>.
|
|
||||||
Also, you have to set both <code>paperWidth</code> and <code>paperHeight</code>.</p>
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.paper_size_and_orientation.c_url" href="#office.paper_size_and_orientation.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>
|
||||||
|
|
||||||
@@ -658,13 +653,11 @@ Also, you have to set both <code>paperWidth</code> and <code>paperHeight</code>.
|
|||||||
</span><span class="se"></span> --url http://localhost:3000/convert/office <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">'Content-Type: multipart/form-data'</span> <span class="se">\
|
</span><span class="se"></span> --header <span class="s1">'Content-Type: multipart/form-data'</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">files</span><span class="o">=</span>@document.docx <span class="se">\
|
||||||
</span><span class="se"></span> --form <span class="nv">paperWidth</span><span class="o">=</span><span class="m">8</span>.27 <span class="se">\
|
|
||||||
</span><span class="se"></span> --form <span class="nv">paperHeight</span><span class="o">=</span><span class="m">11</span>.27 <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> -o result.pdf
|
</span><span class="se"></span> -o result.pdf
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.paper_size_and_orientation.go" href="#office.paper_size_and_orientation.go">
|
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.orientation.go" href="#office.orientation.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>
|
||||||
|
|
||||||
@@ -673,14 +666,13 @@ Also, you have to set both <code>paperWidth</code> and <code>paperHeight</code>.
|
|||||||
<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
|
<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
|
||||||
<span class="nx">c</span> <span class="o">:=</span> <span class="o">&</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">"http://localhost:3000"</span><span class="p">}</span>
|
<span class="nx">c</span> <span class="o">:=</span> <span class="o">&</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">"http://localhost:3000"</span><span class="p">}</span>
|
||||||
<span class="nx">req</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">NewOfficeRequest</span><span class="p">([]</span><span class="kt">string</span><span class="p">{</span><span class="s">"document.docx"</span><span class="p">})</span>
|
<span class="nx">req</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">NewOfficeRequest</span><span class="p">([]</span><span class="kt">string</span><span class="p">{</span><span class="s">"document.docx"</span><span class="p">})</span>
|
||||||
<span class="nx">req</span><span class="p">.</span><span class="nf">SetPaperSize</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">SetLandscape</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">SetLandscape</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
|
||||||
<span class="nx">dest</span> <span class="o">:=</span> <span class="s">"result.pdf"</span>
|
<span class="nx">dest</span> <span class="o">:=</span> <span class="s">"result.pdf"</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>
|
||||||
<span class="p">}</span>
|
<span class="p">}</span>
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.paper_size_and_orientation.php" href="#office.paper_size_and_orientation.php">
|
<h3 class="Heading"><a class="Anchor" aria-hidden="true" id="office.orientation.php" href="#office.orientation.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>
|
||||||
|
|
||||||
@@ -693,7 +685,6 @@ $files = [
|
|||||||
DocumentFactory::makeFromPath('document.docx', 'document.docx'),
|
DocumentFactory::makeFromPath('document.docx', 'document.docx'),
|
||||||
];
|
];
|
||||||
$request = new OfficeRequest($files);
|
$request = new OfficeRequest($files);
|
||||||
$request->setPaperSize(Request::A4);
|
|
||||||
$request->setLandscape(true);
|
$request->setLandscape(true);
|
||||||
$dirPath = "/foo";
|
$dirPath = "/foo";
|
||||||
$filename = $client->store($request, $dirPath);
|
$filename = $client->store($request, $dirPath);
|
||||||
|
|||||||
@@ -38,12 +38,6 @@ func convertOffice(c echo.Context) error {
|
|||||||
return hijackErr(errors.New("no suitable office documents to convert"), r)
|
return hijackErr(errors.New("no suitable office documents to convert"), r)
|
||||||
}
|
}
|
||||||
p := &printer.Office{Context: ctx, FilePaths: fpaths}
|
p := &printer.Office{Context: ctx, FilePaths: fpaths}
|
||||||
paperSize, err := r.paperSize()
|
|
||||||
if err != nil {
|
|
||||||
return hijackErr(err, r)
|
|
||||||
}
|
|
||||||
p.PaperWidth = paperSize[0]
|
|
||||||
p.PaperHeight = paperSize[1]
|
|
||||||
landscape, err := r.landscape()
|
landscape, err := r.landscape()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return hijackErr(err, r)
|
return hijackErr(err, r)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
|
||||||
@@ -17,11 +16,9 @@ var mu sync.Mutex
|
|||||||
|
|
||||||
// Office facilitates Office documents to PDF conversion.
|
// Office facilitates Office documents to PDF conversion.
|
||||||
type Office struct {
|
type Office struct {
|
||||||
Context context.Context
|
Context context.Context
|
||||||
FilePaths []string
|
FilePaths []string
|
||||||
PaperWidth float64
|
Landscape bool
|
||||||
PaperHeight float64
|
|
||||||
Landscape bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print converts Office documents to PDF.
|
// Print converts Office documents to PDF.
|
||||||
@@ -36,15 +33,9 @@ func (o *Office) Print(destination string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tmpDest := fmt.Sprintf("%s/%s.pdf", dirPath, baseFilename)
|
tmpDest := fmt.Sprintf("%s/%s.pdf", dirPath, baseFilename)
|
||||||
paperSize, err := unoconvPaperSize(o.PaperWidth, o.PaperHeight)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cmdArgs := []string{
|
cmdArgs := []string{
|
||||||
"--format",
|
"--format",
|
||||||
"pdf",
|
"pdf",
|
||||||
"--printer",
|
|
||||||
paperSize,
|
|
||||||
}
|
}
|
||||||
if o.Landscape {
|
if o.Landscape {
|
||||||
cmdArgs = append(cmdArgs, "--printer", "PaperOrientation=landscape")
|
cmdArgs = append(cmdArgs, "--printer", "PaperOrientation=landscape")
|
||||||
@@ -70,18 +61,6 @@ func (o *Office) Print(destination string) error {
|
|||||||
return Merge(fpaths, destination)
|
return Merge(fpaths, destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unoconvPaperSize(paperWidth, paperHeight float64) (string, error) {
|
|
||||||
width, err := strconv.Atoi(fmt.Sprintf("%.0f", paperWidth*25.4))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("%0.f: converting width to millimiter: %v", paperWidth, err)
|
|
||||||
}
|
|
||||||
height, err := strconv.Atoi(fmt.Sprintf("%.0f", paperHeight*25.4))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("%0.f: converting height to millimiter: %v", paperHeight, err)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("PaperSize=%dx%d", width, height), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compile-time checks to ensure type implements desired interfaces.
|
// Compile-time checks to ensure type implements desired interfaces.
|
||||||
var (
|
var (
|
||||||
_ = Printer(new(Office))
|
_ = Printer(new(Office))
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ type ChromeRequest interface {
|
|||||||
// conversion requests which will be
|
// conversion requests which will be
|
||||||
// handle by unoconv.
|
// handle by unoconv.
|
||||||
type UnoconvRequest interface {
|
type UnoconvRequest interface {
|
||||||
SetPaperSize(size [2]float64)
|
|
||||||
SetLandscape(landscape bool)
|
SetLandscape(landscape bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,12 +28,6 @@ func (office *OfficeRequest) SetWebhookURL(webhookURL string) {
|
|||||||
office.values[webhookURL] = webhookURL
|
office.values[webhookURL] = webhookURL
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPaperSize sets paperWidth and paperHeight form fields.
|
|
||||||
func (office *OfficeRequest) SetPaperSize(size [2]float64) {
|
|
||||||
office.values[paperWidth] = fmt.Sprintf("%f", size[0])
|
|
||||||
office.values[paperHeight] = fmt.Sprintf("%f", size[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLandscape sets landscape form field.
|
// SetLandscape sets landscape form field.
|
||||||
func (office *OfficeRequest) SetLandscape(isLandscape bool) {
|
func (office *OfficeRequest) SetLandscape(isLandscape bool) {
|
||||||
office.values[landscape] = strconv.FormatBool(isLandscape)
|
office.values[landscape] = strconv.FormatBool(isLandscape)
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ func TestOffice(t *testing.T) {
|
|||||||
test.OfficeTestFilePath(t, "document.docx"),
|
test.OfficeTestFilePath(t, "document.docx"),
|
||||||
})
|
})
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
req.SetPaperSize(A4)
|
|
||||||
req.SetLandscape(false)
|
req.SetLandscape(false)
|
||||||
dirPath, err := rand.Get()
|
dirPath, err := rand.Get()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user