mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-07-02 08:27:41 +08:00
Add flatten option to the chromium convert route
This commit is contained in:
@@ -330,15 +330,19 @@ func convertUrlRoute(chromium Api, engine gotenberg.PdfEngine) api.Route {
|
||||
pdfFormats := pdfengines.FormDataPdfFormats(form)
|
||||
metadata := pdfengines.FormDataPdfMetadata(form, false)
|
||||
|
||||
var url string
|
||||
var (
|
||||
url string
|
||||
flatten bool
|
||||
)
|
||||
err := form.
|
||||
MandatoryString("url", &url).
|
||||
Bool("flatten", &flatten, false).
|
||||
Validate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("validate form data: %w", err)
|
||||
}
|
||||
|
||||
err = convertUrl(ctx, chromium, engine, url, options, mode, pdfFormats, metadata)
|
||||
err = convertUrl(ctx, chromium, engine, url, options, mode, pdfFormats, metadata, flatten)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert URL to PDF: %w", err)
|
||||
}
|
||||
@@ -391,16 +395,20 @@ func convertHtmlRoute(chromium Api, engine gotenberg.PdfEngine) api.Route {
|
||||
pdfFormats := pdfengines.FormDataPdfFormats(form)
|
||||
metadata := pdfengines.FormDataPdfMetadata(form, false)
|
||||
|
||||
var inputPath string
|
||||
var (
|
||||
inputPath string
|
||||
flatten bool
|
||||
)
|
||||
err := form.
|
||||
MandatoryPath("index.html", &inputPath).
|
||||
Bool("flatten", &flatten, false).
|
||||
Validate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("validate form data: %w", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("file://%s", inputPath)
|
||||
err = convertUrl(ctx, chromium, engine, url, options, mode, pdfFormats, metadata)
|
||||
err = convertUrl(ctx, chromium, engine, url, options, mode, pdfFormats, metadata, flatten)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert HTML to PDF: %w", err)
|
||||
}
|
||||
@@ -457,11 +465,13 @@ func convertMarkdownRoute(chromium Api, engine gotenberg.PdfEngine) api.Route {
|
||||
var (
|
||||
inputPath string
|
||||
markdownPaths []string
|
||||
flatten bool
|
||||
)
|
||||
|
||||
err := form.
|
||||
MandatoryPath("index.html", &inputPath).
|
||||
MandatoryPaths([]string{".md"}, &markdownPaths).
|
||||
Bool("flatten", &flatten, false).
|
||||
Validate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("validate form data: %w", err)
|
||||
@@ -472,7 +482,7 @@ func convertMarkdownRoute(chromium Api, engine gotenberg.PdfEngine) api.Route {
|
||||
return fmt.Errorf("transform markdown file(s) to HTML: %w", err)
|
||||
}
|
||||
|
||||
err = convertUrl(ctx, chromium, engine, url, options, mode, pdfFormats, metadata)
|
||||
err = convertUrl(ctx, chromium, engine, url, options, mode, pdfFormats, metadata, flatten)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert markdown to PDF: %w", err)
|
||||
}
|
||||
@@ -596,7 +606,7 @@ func markdownToHtml(ctx *api.Context, inputPath string, markdownPaths []string)
|
||||
return fmt.Sprintf("file://%s", inputPath), nil
|
||||
}
|
||||
|
||||
func convertUrl(ctx *api.Context, chromium Api, engine gotenberg.PdfEngine, url string, options PdfOptions, mode gotenberg.SplitMode, pdfFormats gotenberg.PdfFormats, metadata map[string]interface{}) error {
|
||||
func convertUrl(ctx *api.Context, chromium Api, engine gotenberg.PdfEngine, url string, options PdfOptions, mode gotenberg.SplitMode, pdfFormats gotenberg.PdfFormats, metadata map[string]interface{}, flatten bool) error {
|
||||
outputPath := ctx.GeneratePath(".pdf")
|
||||
|
||||
err := chromium.Pdf(ctx, ctx.Log(), url, outputPath, options)
|
||||
@@ -650,6 +660,13 @@ func convertUrl(ctx *api.Context, chromium Api, engine gotenberg.PdfEngine, url
|
||||
return fmt.Errorf("write metadata: %w", err)
|
||||
}
|
||||
|
||||
if flatten {
|
||||
convertOutputPaths, err = pdfengines.FlattenStub(ctx, engine, outputPaths)
|
||||
if err != nil {
|
||||
return fmt.Errorf("flatten PDFs: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
zeroValuedSplitMode := gotenberg.SplitMode{}
|
||||
zeroValuedPdfFormats := gotenberg.PdfFormats{}
|
||||
if mode != zeroValuedSplitMode && pdfFormats != zeroValuedPdfFormats {
|
||||
|
||||
@@ -1431,6 +1431,7 @@ func TestConvertUrl(t *testing.T) {
|
||||
splitMode gotenberg.SplitMode
|
||||
pdfFormats gotenberg.PdfFormats
|
||||
metadata map[string]interface{}
|
||||
flatten bool
|
||||
expectError bool
|
||||
expectHttpError bool
|
||||
expectHttpStatus int
|
||||
@@ -1669,6 +1670,20 @@ func TestConvertUrl(t *testing.T) {
|
||||
expectError: true,
|
||||
expectHttpError: false,
|
||||
},
|
||||
{
|
||||
scenario: "PDF engine flatten error",
|
||||
ctx: &api.ContextMock{Context: new(api.Context)},
|
||||
api: &ApiMock{PdfMock: func(ctx context.Context, logger *zap.Logger, url, outputPath string, options PdfOptions) error {
|
||||
return nil
|
||||
}},
|
||||
engine: &gotenberg.PdfEngineMock{FlattenMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string) error {
|
||||
return errors.New("bar")
|
||||
}},
|
||||
options: DefaultPdfOptions(),
|
||||
flatten: true,
|
||||
expectError: true,
|
||||
expectHttpError: false,
|
||||
},
|
||||
{
|
||||
scenario: "cannot add output paths",
|
||||
ctx: func() *api.ContextMock {
|
||||
@@ -1697,6 +1712,9 @@ func TestConvertUrl(t *testing.T) {
|
||||
WriteMetadataMock: func(ctx context.Context, logger *zap.Logger, metadata map[string]interface{}, inputPath string) error {
|
||||
return nil
|
||||
},
|
||||
FlattenMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
options: DefaultPdfOptions(),
|
||||
pdfFormats: gotenberg.PdfFormats{PdfA: gotenberg.PdfA1b},
|
||||
@@ -1704,6 +1722,7 @@ func TestConvertUrl(t *testing.T) {
|
||||
"Creator": "foo",
|
||||
"Producer": "bar",
|
||||
},
|
||||
flatten: true,
|
||||
expectError: false,
|
||||
expectHttpError: false,
|
||||
expectOutputPathsCount: 1,
|
||||
@@ -1717,7 +1736,7 @@ func TestConvertUrl(t *testing.T) {
|
||||
tc.ctx.SetPathRename(&gotenberg.PathRenameMock{RenameMock: func(oldpath, newpath string) error {
|
||||
return nil
|
||||
}})
|
||||
err := convertUrl(tc.ctx.Context, tc.api, tc.engine, "", tc.options, tc.splitMode, tc.pdfFormats, tc.metadata)
|
||||
err := convertUrl(tc.ctx.Context, tc.api, tc.engine, "", tc.options, tc.splitMode, tc.pdfFormats, tc.metadata, tc.flatten)
|
||||
|
||||
if tc.expectError && err == nil {
|
||||
t.Fatal("expected error but got none", err)
|
||||
|
||||
Reference in New Issue
Block a user