office_to_pdf

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 5 Imported by: 0

README

Office to PDF Go Module

A reusable Go module for converting Excel (.xlsx), Word (.docx), and Images (.jpg, .png) to PDF using pure Go (MIT-licensed) libraries.

Features

  • Pure Go: No external dependencies like LibreOffice or Docker required.
  • Embedded Font: Includes a default Arial font for Excel rendering, works out of the box.
  • Excel support: Custom robust renderer using excelize and gopdf.
  • Word support: Uses docx2pdf for DOCX conversion.
  • Image support: Convert single or multiple JPG and PNG images to a single PDF.
  • Simple API: Easy to integrate into any project.

Installation

go get github.com/kuyaninja/office-to-pdf

Usage

Converting Excel (.xlsx)

Excel conversion uses an embedded Arial font by default for proper rendering.

package main

import (
    "github.com/kuyaninja/office-to-pdf"
    "log"
)

func main() {
    // Initialize converter (uses embedded font by default)
    converter := office_to_pdf.NewConverter("", "")

    err := converter.ConvertXlsxToPdf("input.xlsx", "output.pdf")
    if err != nil {
        log.Fatal(err)
    }
}
Converting Multiple Images to 1 PDF
package main

import (
    "github.com/kuyaninja/office-to-pdf"
    "log"
)

func main() {
    converter := office_to_pdf.NewConverter("", "")

    images := []string{"page1.jpg", "page2.png", "page3.jpg"}
    err := converter.ConvertImagesToPdf(images, "combined.pdf")
    if err != nil {
        log.Fatal(err)
    }
}
Converting Word (.docx)
package main

import (
    "github.com/kuyaninja/office-to-pdf"
    "log"
)

func main() {
    converter := office_to_pdf.NewConverter("", "")

    err := converter.ConvertDocxToPdf("input.docx", "output.pdf")
    if err != nil {
        log.Fatal(err)
    }
}

CLI Tool

You can also use the included CLI tool:

go build -o convert cmd/convert/main.go

# Convert multiple images
./convert -output combined.pdf photo1.jpg photo2.png photo3.jpg

# Convert single files
./convert data.xlsx
./convert document.docx

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Converter

type Converter struct {
	FontPath   string // Path to a TTF font file
	FontFamily string // Font family name to use in PDF
}

Converter provides methods to convert Office documents and images to PDF using pure Go.

func NewConverter

func NewConverter(fontPath, fontFamily string) *Converter

NewConverter returns a new Converter with optional font settings.

func (*Converter) ConvertDocxToPdf

func (c *Converter) ConvertDocxToPdf(inputPath, outputPath string) error

ConvertDocxToPdf converts a DOCX file to a PDF file.

func (*Converter) ConvertImageToPdf

func (c *Converter) ConvertImageToPdf(inputPath, outputPath string) error

ConvertImageToPdf converts a single image file (JPG, PNG) to a PDF file.

func (*Converter) ConvertImagesToPdf

func (c *Converter) ConvertImagesToPdf(inputPaths []string, outputPath string) error

ConvertImagesToPdf converts multiple image files (JPG, PNG) to a single PDF file (one image per page).

func (*Converter) ConvertXlsxToPdf

func (c *Converter) ConvertXlsxToPdf(inputPath, outputPath string) error

ConvertXlsxToPdf converts an XLSX file to a PDF file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL