libreofficekit

package module
v0.0.0-...-c2f861d Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 7 Imported by: 0

README

go-libreofficekit Build Status Go Report Card codecov

CGo bindings to LibreOfficeKit

Install

# Latest version of LibreOffice (5.2) is required
$ sudo add-apt-repository ppa:libreoffice/ppa 
$ sudo apt-get update
$ sudo apt-get install libreoffice libreofficekit-dev
$ go get github.com/docsbox/go-libreofficekit

Usage

This example demonstrates how to convert Microsoft Office document to PDF

package main

import "github.com/dveselov/go-libreofficekit"

func main() {
    office, _ := libreofficekit.NewOffice("/path/to/libreoffice")
    
    document, _ := office.LoadDocument("kittens.docx")
    document.SaveAs("kittens.pdf", "pdf", "skipImages")

    document.Close()
    office.Close()
}

This example demonstrates how to get presentation slides titles

package main

import "fmt"
import "github.com/dveselov/go-libreofficekit"

func main() {
    office, _ := libreofficekit.NewOffice("/path/to/libreoffice")
    
    document, _ := office.LoadDocument("kittens.pptx")
    slidesCount := document.GetParts()

    for i := 1; i < slidesCount; i++ {
        document.SetPart(i)
        currentPart = document.GetPart()
        fmt.Println("Current slide =", currentPart)
        currentPartName = document.GetPartName(i)
        fmt.Println("Current slide title =", currentPartName)
    }

    document.Close()
    office.Close()
}

Next example demonstrates how to use built-in LibreOffice rendering engine for creating page-by-page documents previews.

package main

import (
    "os"
    "fmt"
    "unsafe"
    "image"
    "image/png"
)
import "github.com/dveselov/go-libreofficekit"

func main() {
    office, _ := libreofficekit.NewOffice("/path/to/libreoffice")
    document, _ := office.LoadDocument("kittens.docx")

    rectangles := document.GetPartPageRectangles()
    canvasWidth := libreofficekit.TwipsToPixels(rectangles[0].Dx(), 120)
    canvasHeight := libreofficekit.TwipsToPixels(rectangles[0].Dy(), 120)

    m := image.NewRGBA(image.Rect(0, 0, canvasWidth, canvasHeight))

    for i, rectangle := range rectangles {
        document.PaintTile(unsafe.Pointer(&m.Pix[0]), canvasWidth, canvasHeight, rectangle.Min.X, rectangle.Min.Y, rectangle.Dx(), rectangle.Dy())
        libreofficekit.BGRA(m.Pix)
        out, _ := os.Create(fmt.Sprintf("page_%v.png", i))
        png.Encode(out, m)
        out.Close()
    }
}

Documentation

Index

Constants

View Source
const (
	TextDocument = iota
	SpreadsheetDocument
	PresentationDocument
	DrawingDocument
	OtherDocument
)

Types of documents returned by Document.GetType function

View Source
const (
	RGBATilemode = iota
	BGRATilemode
)

Types of tile color mode

View Source
const (
	SetGraphicSelectionStart = iota
	SetGraphicSelectionEnd
)

Variables

This section is empty.

Functions

func PixelsToTwips

func PixelsToTwips(pixels int, dpi int) int

PixelsToTwips is like TwipsToPixels, but to another way

func TwipsToPixels

func TwipsToPixels(twips int, dpi int) int

TwipsToPixels converts given twips to pixels with given dpi

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

func (*Document) Close

func (document *Document) Close()

Close destroys document

func (*Document) CreateView

func (document *Document) CreateView() int

CreateView return id if newly created view

func (*Document) GetPart

func (document *Document) GetPart() int

GetPart returns current part of document, e.g. if document was just loaded it's current part will be 0

func (*Document) GetPartName

func (document *Document) GetPartName(part int) string

GetPartName returns current slide title (for presentations) or page title (for text documents)

func (*Document) GetPartPageRectangles

func (document *Document) GetPartPageRectangles() []image.Rectangle

GetPartPageRectangles array of image.Rectangle, with actually TextDocument page rectangles. Useful, when rendering text document page-by-page.

func (*Document) GetParts

func (document *Document) GetParts() int

GetParts returns count of slides (for presentations) or pages (for text documents)

func (*Document) GetSize

func (document *Document) GetSize() (int, int)

GetSize returns width and height of document in twips (1 Twip = 1/1440th of an inch) You can convert twips to pixels by this formula: (width or height) * (1.0 / 1440.0) * DPI

func (*Document) GetTextSelection

func (document *Document) GetTextSelection(mimetype string) string

func (*Document) GetTileMode

func (document *Document) GetTileMode() int

GetTileMode returns tile mode of document, currently only RGBA or BGRA (5.2). You can compare returned int with RGBATilemode / BGRATilemode.

func (*Document) GetType

func (document *Document) GetType() int

GetType returns type of loaded document

func (*Document) GetView

func (document *Document) GetView() int

GetView returns current document view id

func (*Document) InitializeForRendering

func (document *Document) InitializeForRendering(arguments string)

InitializeForRendering must be called before performing any rendering-related actions

func (*Document) PaintTile

func (document *Document) PaintTile(buf unsafe.Pointer, canvasWidth int, canvasHeight int, tilePosX int, tilePosY int, tileWidth int, tileHeight int)

PaintTile renders tile to given buf (which size must be a `4 * canvasWidth * canvasHeight`). In practice buf must be a pointer to image.Image.Pix array's first element, e.g. unsafe.Pointer(&image.Pix[0])

func (*Document) ResetTextSelection

func (document *Document) ResetTextSelection()

func (*Document) SaveAs

func (document *Document) SaveAs(path string, format string, filter string) error

SaveAs saves document at desired path in desired format with applied filter rules Actual (from libreoffice) error message can be read with Office.GetError

func (*Document) SetPart

func (document *Document) SetPart(part int)

SetPart updates current part of document

func (*Document) SetTextSelection

func (document *Document) SetTextSelection(sType int, x int, y int)

type Office

type Office struct {
	Mutex *sync.Mutex
	// contains filtered or unexported fields
}

func NewOffice

func NewOffice(path string) (*Office, error)

NewOffice returns new Office or error if LibreOfficeKit fails to load required libs (actually, when libreofficekit-dev package isn't installed or path is invalid)

func NewOfficeWithProfile

func NewOfficeWithProfile(path, profile string) (*Office, error)

NewOfficeWithPath returns new Office or error if LibreOfficeKit fails to load required libs (actually, when libreofficekit-dev package isn't installed or path is invalid) profile path should already exist

func (*Office) Close

func (office *Office) Close()

Close destroys C LibreOfficeKit instance

func (*Office) GetError

func (office *Office) GetError() string

GetError returns last happened error message in human-readable format

func (*Office) GetFilters

func (office *Office) GetFilters() string

func (*Office) LoadDocument

func (office *Office) LoadDocument(path string) (*Document, error)

LoadDocument return Document or error, if LibreOffice fails to open document at provided path. Actual error message can be retrieved by office.GetError method

Jump to

Keyboard shortcuts

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