Code Monkey home page Code Monkey logo

go-libreofficekit's Introduction

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()
    }
}

go-libreofficekit's People

Contributors

dveselov avatar acaloiaro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.