1. Products
  2.   PDF
  3.   GO
  4.   unipdf  
 
  

Go API for PDF Document Creation & Processing 

Open Source Go Library that gives developers the ability to Create, Edit, Manipulate & Convert PDF Documents.

The unipdf is an open Source Go library that can easily handle all of your queries related to PDF document creation and manipulation. The library is available under dual license where AGPL license can be used for open source software development. The library provides complete support for generating PDF documents with a high level of text, drawings, and images.

The open source unipdf library supports several common as well as advanced features for processing and optimizing PDF documents, such as creating PDF reports, PDF reports generation, creating invoices, paragraph creation and handling, merging and splitting PDF pages, rotating pages, extracting text from PDF files, export PDF to CSV, convert Images to PDF, compress and optimize PDF, watermark PDF files, composite fonts (Unicode characters) support, digital signatures and many more.

.

Previous Next

Getting Started with unipdf

To install the unipdf on your system, please run the following command. 

Install gofpdf via GitHub

go get github.com/unidoc/unipdf/v3

For the latest update please visit the product releases page .

Generate PDF Report via Go API

The open source unipdf API provides developers the capability to create PDF reports inside their own Go applications.  The library allows developers to efficiently work with PDF documents and has included support for adding images, tables, headers, footers, and a lot more into your PDF reports with ease. You can also place dynamic content into your PDF reports with just a couple of lines of code.

Convert PDF File to CSV via Go API

The unipdf library enables software developers to convert PDF files to CSV file formats inside their own Go applications. PDF to Excel converters is a very useful tool for business and research institutions. It’s very easy to use and provides the capability to extract TextMarks from PDF, and group them together into words, rows, and columns for CSV data extraction.

Insert Images into PDF

The open source unipdf API gives software programmers the capability to add images of their own choice into PDF documents inside GO applications. It makes developer's jobs easier while placing images in the PDF document, without having to worry about coordinates. You just need to provide the image path and size without worrying about the coordinates. The library has included support for popular image formats such as JPEG, PNG, GIF, TIFF, and more.

 // Images to PDF.
func imagesToPdf(inputPaths []string, outputPath string) error {
	c := creator.New()

	for _, imgPath := range inputPaths {
		common.Log.Debug("Image: %s", imgPath)

		img, err := c.NewImageFromFile(imgPath)
		if err != nil {
			common.Log.Debug("Error loading image: %v", err)
			return err
		}
		img.ScaleToWidth(612.0)

		// Use page width of 612 points, and calculate the height proportionally based on the image.
		// Standard PPI is 72 points per inch, thus a width of 8.5"
		height := 612.0 * img.Height() / img.Width()
		c.SetPageSize(creator.PageSize{612, height})
		c.NewPage()
		img.SetPos(0, 0)
		_ = c.Draw(img)
	}

	err := c.WriteToFile(outputPath)
	return err
}
  

Add Password to PDF Documents

The free unipdf API gives developers the power to protect their PDF documents by applying a password to them using Go commands. You can restrict users from opening and reading PDF documents. You can also set the owner password to grant full access to the PDF file. Moreover, you can also restrict users from making any sort of changes to certain parts of PDF documents.

func protectPdf(inputPath string, outputPath string, userPassword, ownerPassword string) error {
	permissions := security.PermPrinting | // Allow printing with low quality
		security.PermFullPrintQuality |
		security.PermModify | // Allow modifications.
		security.PermAnnotate | // Allow annotations.
		security.PermFillForms |
		security.PermRotateInsert | // Allow modifying page order, rotating pages etc.
		security.PermExtractGraphics | // Allow extracting graphics.
		security.PermDisabilityExtract // Allow extracting graphics (accessibility)

	encryptOptions := &model.EncryptOptions{
		Permissions: permissions,
	}

	f, err := os.Open(inputPath)
	if err != nil {
		return err
	}
	defer f.Close()

	pdfReader, err := model.NewPdfReader(f)
	if err != nil {
		return err
	}

	isEncrypted, err := pdfReader.IsEncrypted()
	if err != nil {
		return err
	}
	if isEncrypted {
		return fmt.Errorf("The PDF is already locked (need to unlock first)")
	}

	// Generate a PdfWriter instance from existing PdfReader.
	pdfWriter, err := pdfReader.ToWriter(nil)
	if err != nil {
		return err
	}

	// Encrypt document before writing to file.
	err = pdfWriter.Encrypt([]byte(userPassword), []byte(ownerPassword), encryptOptions)
	if err != nil {
		return err
	}

	// Write to file.
	err = pdfWriter.WriteToFile(outputPath)
	return err
}
  
 
 English