package api import ( "context" "errors" "fmt" "log/slog" "os" "os/exec" "strings" "syscall" "time" "github.com/alexliesenfeld/health" flag "github.com/spf13/pflag" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/metric" "go.uber.org/multierr" "github.com/gotenberg/gotenberg/v8/pkg/gotenberg" "github.com/gotenberg/gotenberg/v8/pkg/modules/api" ) func init() { gotenberg.MustRegisterModule(new(Api)) } var ( // ErrInvalidPdfFormats happens if LibreOffice cannot handle the PDF // formats option. ErrInvalidPdfFormats = errors.New("invalid PDF formats") // ErrUnoException happens when unoconverter returns exit code 5. ErrUnoException = errors.New("uno exception") // ErrRuntimeException happens when unoconverter returns exit code 6. ErrRuntimeException = errors.New("uno exception") // ErrCoreDumped happens randomly; sometimes a conversion will work as // expected, and some other time the same conversion will fail. // See https://github.com/gotenberg/gotenberg/issues/639. ErrCoreDumped = errors.New("core dumped") ) // Api is a module that provides a [Uno] to interact with LibreOffice. type Api struct { autoStart bool args libreOfficeArguments logger *slog.Logger libreOffice libreOffice supervisor gotenberg.ProcessSupervisor reqsCounter metric.Int64Counter errsCounter metric.Int64Counter conversionDurationCounter metric.Float64Histogram queueWaitDurationCounter metric.Float64Histogram pdfOutputSizeCounter metric.Int64Histogram } // Options gathers available options when converting a document to PDF. // See: https://help.libreoffice.org/latest/en-US/text/shared/guide/pdf_params.html. type Options struct { // Password specifies the password for opening the source file. Password string // #nosec // Landscape allows changing the orientation of the resulting PDF. Landscape bool // PageRanges allows selecting the pages to convert. PageRanges string // UpdateIndexes specifies whether to update the indexes before conversion, // keeping in mind that doing so might result in missing links in the final // PDF. UpdateIndexes bool // ExportFormFields specifies whether form fields are exported as widgets // or only their fixed print representation is exported. ExportFormFields bool // AllowDuplicateFieldNames specifies whether multiple form fields exported // are allowed to have the same field name. AllowDuplicateFieldNames bool // ExportBookmarks specifies if bookmarks are exported to PDF. ExportBookmarks bool // ExportBookmarksToPdfDestination specifies that the bookmarks contained // in the source LibreOffice file should be exported to the PDF file as // Named Destination. ExportBookmarksToPdfDestination bool // ExportPlaceholders exports the placeholder fields visual markings only. // The exported placeholder is ineffective. ExportPlaceholders bool // ExportNotes specifies if notes are exported to PDF. ExportNotes bool // ExportNotesPages specifies if notes pages are exported to PDF. // Notes pages are available in Impress documents only. ExportNotesPages bool // ExportOnlyNotesPages specifies if the property ExportNotesPages is set // to true if only notes pages are exported to PDF. ExportOnlyNotesPages bool // ExportNotesInMargin specifies if notes in the margin are exported to // PDF. ExportNotesInMargin bool // ConvertOooTargetToPdfTarget specifies that the target documents with // .od[tpgs] extension will have that extension changed to .pdf when the // link is exported to PDF. The source document remains untouched. ConvertOooTargetToPdfTarget bool // ExportLinksRelativeFsys specifies that the file system related // hyperlinks (file:// protocol) present in the document will be exported // as relative to the source document location. ExportLinksRelativeFsys bool // ExportHiddenSlides exports, for LibreOffice Impress, slides that are not // included in slide shows. ExportHiddenSlides bool // SkipEmptyPages specifies that automatically inserted empty pages are // suppressed. This option is active only if storing Writer documents. SkipEmptyPages bool // AddOriginalDocumentAsStream specifies that a stream is inserted to the // PDF file which contains the original document for archiving purposes. AddOriginalDocumentAsStream bool // SinglePageSheets ignores each sheet’s paper size, print ranges and // shown/hidden status and puts every sheet (even hidden sheets) on exactly // one page. SinglePageSheets bool // LosslessImageCompression specifies if images are exported to PDF using // a lossless compression format like PNG or compressed using the JPEG // format. LosslessImageCompression bool // Quality specifies the quality of the JPG export. A higher value produces // a higher-quality image and a larger file. Between 1 and 100. Quality int // ReduceImageResolution specifies if the resolution of each image is // reduced to the resolution specified by the property MaxImageResolution. ReduceImageResolution bool // MaxImageResolution, if the property ReduceImageResolution is set to // true, tells if all images will be reduced to the given value in DPI. // Possible values are: 75, 150, 300, 600 and 1200. MaxImageResolution int // PdfFormats allows to convert the resulting PDF to PDF/A-1b, PDF/A-2b, // PDF/A-3b and PDF/UA. PdfFormats gotenberg.PdfFormats } // DefaultOptions returns the default values for Options. func DefaultOptions() Options { return Options{ Password: "", Landscape: false, PageRanges: "", UpdateIndexes: true, ExportFormFields: true, AllowDuplicateFieldNames: false, ExportBookmarks: true, ExportBookmarksToPdfDestination: false, ExportPlaceholders: false, ExportNotes: false, ExportNotesPages: false, ExportOnlyNotesPages: false, ExportNotesInMargin: false, ConvertOooTargetToPdfTarget: false, ExportLinksRelativeFsys: false, ExportHiddenSlides: false, SkipEmptyPages: false, AddOriginalDocumentAsStream: false, SinglePageSheets: false, LosslessImageCompression: false, Quality: 90, ReduceImageResolution: false, MaxImageResolution: 300, PdfFormats: gotenberg.PdfFormats{ PdfA: "", PdfUa: false, }, } } // Uno is an abstraction on top of the Universal Network Objects API. type Uno interface { Pdf(ctx context.Context, logger *slog.Logger, inputPath, outputPath string, options Options) error Extensions() []string } // Provider is a module interface that exposes a method for creating a // [Uno] for other modules. // // func (m *YourModule) Provision(ctx *gotenberg.Context) error { // provider, _ := ctx.Module(new(libreofficeapi.Provider)) // libreOffice, _ := provider.(api.Provider).LibreOffice() // } type Provider interface { LibreOffice() (Uno, error) } // Descriptor returns a [Api]'s module descriptor. func (a *Api) Descriptor() gotenberg.ModuleDescriptor { return gotenberg.ModuleDescriptor{ ID: "libreoffice-api", FlagSet: func() *flag.FlagSet { fs := flag.NewFlagSet("api", flag.ExitOnError) fs.Int64("libreoffice-restart-after", 10, "Number of conversions after which LibreOffice will automatically restart. Set to 0 to disable this feature") fs.Int64("libreoffice-max-queue-size", 0, "Maximum request queue size for LibreOffice. Set to 0 to disable this feature") fs.Bool("libreoffice-auto-start", false, "Automatically launch LibreOffice upon initialization if set to true; otherwise, LibreOffice will start at the time of the first conversion") fs.Duration("libreoffice-start-timeout", time.Duration(20)*time.Second, "Maximum duration to wait for LibreOffice to start or restart") return fs }(), New: func() gotenberg.Module { return new(Api) }, } } // Provision sets the module properties. func (a *Api) Provision(ctx *gotenberg.Context) error { flags := ctx.ParsedFlags() a.autoStart = flags.MustBool("libreoffice-auto-start") libreOfficeBinPath, ok := os.LookupEnv("LIBREOFFICE_BIN_PATH") if !ok { return errors.New("LIBREOFFICE_BIN_PATH environment variable is not set") } unoBinPath, ok := os.LookupEnv("UNOCONVERTER_BIN_PATH") if !ok { return errors.New("UNOCONVERTER_BIN_PATH environment variable is not set") } a.args = libreOfficeArguments{ binPath: libreOfficeBinPath, unoBinPath: unoBinPath, startTimeout: flags.MustDuration("libreoffice-start-timeout"), } // Logger. a.logger = gotenberg.Logger(a).With(slog.String("logger", "libreoffice")) // Process. a.libreOffice = newLibreOfficeProcess(a.args) a.supervisor = gotenberg.NewProcessSupervisor(a.logger, a.libreOffice, flags.MustInt64("libreoffice-restart-after"), flags.MustInt64("libreoffice-max-queue-size"), 1) // OpenTelemetry. meter := gotenberg.Meter() _, err := meter.Int64ObservableCounter( "libreoffice.process.restarts.total", metric.WithDescription("Current number of LibreOffice restarts."), metric.WithUnit("{restart}"), metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { val := a.supervisor.RestartsCount() o.Observe(val) return nil }), ) if err != nil { return fmt.Errorf("create process restarts observable counter: %w", err) } _, err = meter.Int64ObservableGauge( "libreoffice.requests.queue_size", metric.WithDescription("Current number of LibreOffice conversion requests waiting to be treated."), metric.WithUnit("{request}"), metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { val := a.supervisor.ReqQueueSize() o.Observe(val) return nil }), ) if err != nil { return fmt.Errorf("create requests queue size observable gauge: %w", err) } _, err = meter.Int64ObservableGauge( "libreoffice.requests.active", metric.WithDescription("Current number of LibreOffice conversion requests actively being processed."), metric.WithUnit("{request}"), metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error { val := a.supervisor.ActiveTasksCount() o.Observe(val) return nil }), ) if err != nil { return fmt.Errorf("create requests active observable gauge: %w", err) } a.reqsCounter, err = meter.Int64Counter( "libreoffice.requests.total", metric.WithDescription("Total number of LibreOffice conversion requests."), metric.WithUnit("{request}"), ) if err != nil { return fmt.Errorf("create requests total counter: %w", err) } a.errsCounter, err = meter.Int64Counter( "libreoffice.errors.total", metric.WithDescription("Total number of LibreOffice errors."), metric.WithUnit("{error}"), ) if err != nil { return fmt.Errorf("create errors total counter: %w", err) } a.conversionDurationCounter, err = meter.Float64Histogram( "libreoffice.conversion.duration", metric.WithDescription("Duration of each PDF conversion."), metric.WithUnit("s"), metric.WithExplicitBucketBoundaries(0.5, 1, 2, 5, 10, 30, 60), ) if err != nil { return fmt.Errorf("create conversion duration histogram: %w", err) } a.queueWaitDurationCounter, err = meter.Float64Histogram( "libreoffice.queue.wait.duration", metric.WithDescription("Time a request spends waiting in the queue before processing starts."), metric.WithUnit("s"), metric.WithExplicitBucketBoundaries(0.5, 1, 2, 5, 10, 30, 60), ) if err != nil { return fmt.Errorf("create queue wait duration histogram: %w", err) } a.pdfOutputSizeCounter, err = meter.Int64Histogram( "libreoffice.pdf.output.size", metric.WithDescription("Size of the generated PDF files."), metric.WithUnit("By"), ) if err != nil { return fmt.Errorf("create pdf output size histogram: %w", err) } return nil } // Validate validates the module properties. func (a *Api) Validate() error { var err error _, statErr := os.Stat(a.args.binPath) if os.IsNotExist(statErr) { err = multierr.Append(err, fmt.Errorf("LibreOffice binary path does not exist: %w", statErr)) } _, statErr = os.Stat(a.args.unoBinPath) if os.IsNotExist(statErr) { err = multierr.Append(err, fmt.Errorf("unoconverter binary path does not exist: %w", statErr)) } return err } // Start does nothing if auto-start is not enabled. Otherwise, it starts a // LibreOffice instance. func (a *Api) Start() error { if !a.autoStart { return nil } err := a.supervisor.Launch() if err != nil { return fmt.Errorf("launch supervisor: %w", err) } return nil } // StartupMessage returns a custom startup message. func (a *Api) StartupMessage() string { if !a.autoStart { return "LibreOffice ready to start" } return "LibreOffice automatically started" } // Stop stops the current browser instance. func (a *Api) Stop(ctx context.Context) error { // Block until the context is done so that another module may gracefully // stop before we do a shutdown. a.logger.DebugContext(ctx, "wait for the end of grace duration") <-ctx.Done() err := a.supervisor.Shutdown() if err == nil { return nil } return fmt.Errorf("stop LibreOffice: %w", err) } // Debug returns additional debug data. func (a *Api) Debug() map[string]any { debug := make(map[string]any) cmd := exec.Command(a.args.binPath, "--version") //nolint:gosec cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} output, err := cmd.Output() if err != nil { debug["version"] = err.Error() return debug } debug["version"] = strings.TrimSpace(string(output)) return debug } // Checks adds a health check that verifies if LibreOffice is healthy. func (a *Api) Checks() ([]health.CheckerOption, error) { return []health.CheckerOption{ health.WithCheck(health.Check{ Name: "libreoffice", Check: func(_ context.Context) error { if a.supervisor.Healthy() { return nil } return errors.New("LibreOffice is unhealthy") }, }), }, nil } // Ready returns no error if the module is ready. func (a *Api) Ready() error { if !a.autoStart { return nil } ctx, cancel := context.WithTimeout(context.Background(), a.args.startTimeout) defer cancel() ticker := time.NewTicker(time.Duration(100) * time.Millisecond) for { select { case <-ctx.Done(): ticker.Stop() return fmt.Errorf("context done while waiting for LibreOffice to be ready: %w", ctx.Err()) case <-ticker.C: ok := a.libreOffice.Healthy(a.logger) if ok { ticker.Stop() return nil } continue } } } // LibreOffice returns a [Uno] for interacting with LibreOffice. func (a *Api) LibreOffice() (Uno, error) { return a, nil } // Pdf converts a document to PDF. func (a *Api) Pdf(ctx context.Context, logger *slog.Logger, inputPath, outputPath string, options Options) error { ctx, span := gotenberg.Tracer().Start(ctx, "LibreOffice.Pdf") defer span.End() start := time.Now() var conversionStart time.Time err := a.supervisor.Run(ctx, logger, func() error { conversionStart = time.Now() return a.libreOffice.pdf(ctx, logger, inputPath, outputPath, options) }) end := time.Now() status := "success" if err != nil { if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) { status = "timeout" } else { status = "error" } } if !conversionStart.IsZero() { waitDuration := conversionStart.Sub(start).Seconds() conversionDuration := end.Sub(conversionStart).Seconds() a.queueWaitDurationCounter.Record(ctx, waitDuration, metric.WithAttributes( attribute.String("status", status), )) a.conversionDurationCounter.Record(ctx, conversionDuration, metric.WithAttributes( attribute.String("status", status), )) } else { waitDuration := end.Sub(start).Seconds() a.queueWaitDurationCounter.Record(ctx, waitDuration, metric.WithAttributes( attribute.String("status", status), )) } a.reqsCounter.Add(ctx, 1, metric.WithAttributes( attribute.String("status", status), )) if err == nil { if fileInfo, statErr := os.Stat(outputPath); statErr == nil { a.pdfOutputSizeCounter.Record(ctx, fileInfo.Size()) } return nil } reason := "unknown" if errors.Is(err, context.DeadlineExceeded) { reason = "timeout" } else if errors.Is(err, context.Canceled) { reason = "context_cancelled" } else if errors.Is(err, ErrInvalidPdfFormats) { reason = "invalid_input" } else if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { reason = "libreoffice_maximum_queue_size_exceeded" } else if errors.Is(err, gotenberg.ErrProcessAlreadyRestarting) { reason = "libreoffice_unavailable" } a.errsCounter.Add(ctx, 1, metric.WithAttributes( attribute.String("reason", reason), )) // See https://github.com/gotenberg/gotenberg/issues/639. if errors.Is(err, ErrCoreDumped) { logger.DebugContext(ctx, fmt.Sprintf("got a '%s' error, retry conversion", err)) err = a.Pdf(ctx, logger, inputPath, outputPath, options) if err != nil { span.RecordError(err) span.SetStatus(codes.Error, err.Error()) } return err } err = fmt.Errorf("supervisor run task: %w", err) span.RecordError(err) span.SetStatus(codes.Error, err.Error()) return err } // Extensions returns the file extensions available for conversions. // FIXME: don't care, take all on the route level? func (a *Api) Extensions() []string { return []string{ ".123", ".602", ".abw", ".bib", ".bmp", ".cdr", ".cgm", ".cmx", ".csv", ".cwk", ".dbf", ".dif", ".doc", ".docm", ".docx", ".dot", ".dotm", ".dotx", ".dxf", ".emf", ".eps", ".epub", ".fodg", ".fodp", ".fods", ".fodt", ".fopd", ".gif", ".htm", ".html", ".hwp", ".jpeg", ".jpg", ".key", ".ltx", ".lwp", ".mcw", ".met", ".mml", ".mw", ".numbers", ".odd", ".odg", ".odm", ".odp", ".ods", ".odt", ".otg", ".oth", ".otp", ".ots", ".ott", ".pages", ".pbm", ".pcd", ".pct", ".pcx", ".pdb", ".pdf", ".pgm", ".png", ".pot", ".potm", ".potx", ".ppm", ".pps", ".ppt", ".pptm", ".pptx", ".psd", ".psw", ".pub", ".pwp", ".pxl", ".ras", ".rtf", ".sda", ".sdc", ".sdd", ".sdp", ".sdw", ".sgl", ".slk", ".smf", ".stc", ".std", ".sti", ".stw", ".svg", ".svm", ".swf", ".sxc", ".sxd", ".sxg", ".sxi", ".sxm", ".sxw", ".tga", ".tif", ".tiff", ".txt", ".uof", ".uop", ".uos", ".uot", ".vdx", ".vor", ".vsd", ".vsdm", ".vsdx", ".wb2", ".wk1", ".wks", ".wmf", ".wpd", ".wpg", ".wps", ".xbm", ".xhtml", ".xls", ".xlsb", ".xlsm", ".xlsx", ".xlt", ".xltm", ".xltx", ".xlw", ".xml", ".xpm", ".zabw", } } // Interface guards. var ( _ gotenberg.Module = (*Api)(nil) _ gotenberg.Provisioner = (*Api)(nil) _ gotenberg.Validator = (*Api)(nil) _ gotenberg.App = (*Api)(nil) _ gotenberg.Debuggable = (*Api)(nil) _ api.HealthChecker = (*Api)(nil) _ Uno = (*Api)(nil) _ Provider = (*Api)(nil) )