GithubHelp home page GithubHelp logo

pdft's Introduction

PDFT

PDFT is a GO library for creating PDF documents using existing PDFs as template. This library depend on gopdf.

Tested with PDF template files created from Libre office, Google Docs, Microsoft Word.

SAMPLE

var pt pdft.PDFt
err := pt.Open(pdfsource)
if err != nil {
	panic("Couldn't open pdf.")
}


err = pt.AddFont("arial", "./arial.ttf")
if err != nil {
    t.Error(err)
    return
}

err = pt.SetFont("arial", "", 14)
if err != nil {
    panic(err) 
}

//insert text to pdf
err = pt.Insert("Hi", 1, 10, 10, 100, 100, gopdf.Center|gopdf.Bottom, nil)
if err != nil {
    panic(err) 
}

// insert text to pdf with color
err = pt.Insert("Hi", 1, 10, 10, 100, 100, gopdf.Center|gopdf.Bottom, &FontColor{R: 255, G: 255, B: 255})
if err != nil {
    panic(err) 
}

// measure text width
var textWidth float64
textWidth, err = pt.MeasureTextWidth("Hi")

// read image file
pic, err := ioutil.ReadFile(picPath)
if err != nil {
	panic("Couldn't read pic.")
}

 
// insert image to pdf
err = pt.InsertImg(pic, 1, 182.0, 165.0, 172.0, 49.0)
if err != nil {
	panic("Couldn't insert image")
}

// insert image to pdf with cache, avoiding redundant data when inserting same images many times
err = pt.InsertImgWithCache(pic, 1, 182.0, 165.0, 172.0, 49.0)
if err != nil {
	panic("Couldn't insert image")
}

// Duplicate first page to last page
err = pt.DuplicatePageAfter(1, -1)
if err != nil {
	panic("Couldn't duplicate first page")
}

// Remove second page
err = pt.RemovePage(2)
if err != nil {
	panic("Couldn't remove targetPage")
}

err = pt.Save(target)
if err != nil {
	panic("Couldn't save pdf.")
}

pdft's People

Contributors

8pg avatar andrerenaud avatar chakrit avatar hanfengsan avatar juaismar avatar kapong avatar karelbilek avatar kelvinsantos avatar oneplus1000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pdft's Issues

Pdf data text length

Text data only filling a portion of the generated pdf, about 50% of the page width regardless of the align value passed in the insert function.

unable to insert image in a fillable pdf

when trying to open a pdf containing form fields it is showing me an error "trailer not found", i have tried to insert image even in a normal pdf and i got the output pdf but the format of output pdf has changed, so this library is altering or changing the format of the input pdf.

Unable to add image to certain pages

I can only insert image to the first page.

When i set the page to 2 the page will not have the image on it but the first one will work

pdf.InsertImg(imgBody, 2, float64(10), float64(10), float64(100), float64(100))

save out pdf errors

func (c *crawlResultFonts) parse(propVal *[]byte) error {
var props PDFObjPropertiesData
err := readProperties(propVal, &props)
if err != nil {
return err
}

for _, prop := range props {
	var crFont crawlResultFont
	fontIndex, err := strconv.Atoi(strings.TrimSpace(strings.Replace(prop.key, "F", "", -1)))
	//fontIndex, err := strconv.Atoi(strings.TrimSpace(strings.Replace(strings.Replace(prop.key, "C", "", -1), "_", "", -1)))
	if err != nil {
		fmt.Println(err.Error(), "strconv.Atoi fontIndex", prop.key)
		return err
	}
	objID, _, err := prop.asDictionary()
	if err != nil {
		return err
	}
	crFont.fontIndex = fontIndex
	crFont.fontObjID = objID
	*c = append(*c, crFont)
}
return nil

}

Insert text is not working

That is my code below. However the text is not showing. The image is showing properly.
Any help? Thanks.

func main() {
	var pt pdft.PDFt
	err := pt.Open("abc.pdf")
	if err != nil {
		panic("Couldn't open pdf.")
	}

	err = pt.AddFont("arial", "./arial.ttf")
	if err != nil {
		fmt.Println(err)
		return
	}

	err = pt.SetFont("arial", "", 14)
	if err != nil {
		panic(err)
	}

	err = pt.Insert("Hello World", 1, 100, 100, 100, 100, gopdf.Center|gopdf.Bottom)
	if err != nil {
		panic(err)
	}

	//read image file
	// pic, err := ioutil.ReadFile("./testlb.png")
	// if err != nil {
	// 	panic("Couldn't read pic.")
	// }

	//insert image to pdf
	// err = pt.InsertImg(pic, 1, 100, 100, 172.0, 49.0)
	// if err != nil {
	// 	panic("Couldn't insert image")
	// }

	pt.Save("hello.pdf")
}

GUI editor

This allows to create a pdf editor because the library allows modifying existing PDF's. Great work !

Is anyone working on making a GUI ?
I think it would not be a huge effort now.

I assume I can just display as an image in the GUI and collect the additions by the users in another image layer. Can the lib return to me a image of a pdf ?

Insert Text becomes upside down

image

I followed the example given but produce this error. Anyone have similar problem?

err = pt.AddFont("arial", "./arial.ttf")
if err != nil {
return err
}

err = pt.SetFont("arial", "", 14)
if err != nil {
return err
}

//insert text to pdf
err = pt.Insert(fullname, 1, 100, 100, 100, 100, pdft.Regular)
if err != nil {
return err
}

Can I Add page from another pdf into current pdf object

Can I Add page from another pdf into current pdf object?

น่าจะเข้าใจภาษาไทย ขออนุญาติพิมพ์เป็นไทย ผมจะสามารถเพิ่มหน้าใหม่เข้ามาได้มั๊ยครับ เช่น จะทำใบเสร็จรับเงิน แต่อาจจะมี 2 แผ่น หรือ 3 แผ่น สามารถทำได้มั๊ยครับ

Library is slow

I am using this code to insert a text into the file:

	var pt pdft.PDFt
	if err := pt.Open("./player-id-template.pdf"); err != nil {
		return "", err
	}

	if err := pt.AddFont("avenir", "./player-id-font.ttf"); err != nil {
		return "", err
	}

	if err := pt.SetFont("avenir", "", 60); err != nil {
		return "", err
	}

	// These numbers have been found by experimenting with PDF template
	// This PDF template will remain always the same
	if err := pt.Insert(id, 1, 0, 525, 510, 100, gopdf.Middle|gopdf.Center); err != nil {
		return "", err
	}

	if err := pt.Save(fileName); err != nil {
		return "", err
	}

It ttakes on average 1.5s to run. Do you know what can be the reason?

I found that pt.Open takes most of the time.

Find and Replace Text

Cant seem to find a way to search a PDF for Text and replace it.

DO you think its possible i could add it and make a PR ?

Hi I can't fix this bug

This is my code

package main

import (
	"fmt"
	"io/ioutil"

	"github.com/signintech/pdft"
)

func main() {

	target := "out_pdf.pdf"
	pdfsource := "Bussiness.pdf"
	picPath :=  "17.jpg"

	var ipdf pdft.PDFt
	err := ipdf.Open(pdfsource)
	if err != nil {
		panic(err)
	}

	pic, err := ioutil.ReadFile(picPath)
	if err != nil {
		panic("Couldn't read pic.")
	}

	//insert image to pdf
	err = ipdf.InsertImg(pic, 1, 1.0, 1.0, 600.0, 600.0)
	if err != nil {
		panic("Couldn't insert image")
	}

	err = ipdf.Save(target)
	if err != nil {
		fmt.Println(err)
		panic("Couldn't save pdf.")
	}
}
not found /Resources in /Type/Pages
panic: Couldn't save pdf.

goroutine 1 [running]:
main.main()
	/Users/pathbox/code/learning-go/src/pdf-power/pdft-example.go:36 +0x1bf
exit status 2

Can you help me

Duplicate pdf

Duplicating the same page multiple times stops working after several times.

E.g. I need to copy the page 10 times, and after the 7th page added it stops working, no matter how often I call the function

Insert text error

err = ipdf.Insert("abcdefg", 1, 30, 20, 100, 100, gopdf.Center|gopdf.Bottom)
if err != nil {
	panic(err)
}

when I insert data abcdefg, The pdf display with DEFGHIJ, I have no idea

trailer regex failed

ishmeets@ishmeets-mbp pdft % go run main.go 
trailer not found
panic: Couldn't open pdf.

goroutine 1 [running]:
main.main()
	/Users/ishmeets/workspace/pdf/pdft/main.go:15 +0x1dc
exit status 2

PDF:
Get_Started_With_Smallpdf.pdf

inserting on 2nd page seems to corrupt data

This library has been very nice, inserting data on the first page works great, but I have had some issues when inserting data on the second page of the pdf.

I was originally trying to insert images onto the second page, but after executing the program, no images are outputted. I tried to insert them onto the first page, and that worked as expected.

This made me curious, so I tried to insert some text onto the second page and something weird happened

when I do something like

    size, err := pt.MeasureTextWidth("hello")
    if err != nil {
        return err
    }
    err = pt.Insert("hello", 2, 0, 0, size, 100, gopdf.Center|gopdf.Bottom)
    if err != nil {
        return err
    }

The text is inserted in the correct place, but the text that is inserted is "K H O O R" and not "hello".

Note: the second page exists when loading the pdf, but is completely blank before any insertions, could this be the problem?

Insert image doesn't work

Hi I'm trying to insert a png image to pdf without success:

    var ipdf pdft.PDFt
err := ipdf.Open(source)
if err != nil {
	panic("Couldn't open pdf.")
}

payload, err := ioutil.ReadFile(signature)
if err != nil {
	panic("Couldn't read signature.")
}

encoded := base64.StdEncoding.EncodeToString(payload)
fmt.Println(encoded)

err = ipdf.InsertImgBase64(encoded, 1, 182.0, 165.0, 172.0, 49.0)
if err != nil {
	panic("Couldn't insert image")
}

err = ipdf.Save(target)
if err != nil {
	panic("Couldn't save pdf.")
}

Fail spliting Font

Hi,

Debugging an error in a try of this module, I found that in file crawl_result_font.go, the function splitFont fail in strconv.Atoi(s[i:]), been s[i:] = "T1_0"

I shared an image showing the error

image

Regards

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.