Free C PDF API to Generate New PDFs & Add Images/Barcodes.
A Powerful Open Source Lghtweight C PDF Processing Library for Generating and Managing PDF Documents. It Support Adding Text, Images, Barcodes and Shape to PDF Documents for Free
What is PDFGen Library?
Creating and managing PDF documents programmatically is a common requirement in the field of software development. From generating invoices and reports to creating complex documents, a reliable PDF generation library is a crucial tool in software developer's arsenal. While many feature-rich libraries exist, they often come with the overhead of complex dependencies and a steep learning curve. This is where the PDFGen C library shines. A small, self-contained, open source C library designed for PDF generation and management. With a minimal footprint and zero external dependencies, it’s ideal for simple applications, embedded systems, or cases where you want to avoid pulling in a heavyweight PDF library. PDFGen offers a solid feature set despite its compact codebase, such as adding text to PDF, working with basic vector graphics in PDFs, embedding barcodes, embedding images, adding bookmarks, adding new pages and so on.
In an ecosystem where libraries can be overwhelmingly complex, PDFGen takes a refreshing approach. The entire library is contained within a single C file and its corresponding header file. This makes integration into any project incredibly simple. DFGen is self-contained and does not rely on any external libraries. This eliminates the headache of managing dependencies and ensures a smooth compilation process. Due to its small footprint and single-file nature, it is perfect for embedding in larger applications where you need basic PDF output without a large overhead. The library is released under the Unlicense, which is a public domain equivalent license. This means you are free to use, modify, and distribute the code for any purpose, commercial or non-commercial, without any restrictions.
Getting Started with PDFGen
The recommend way to install PDFGen is using GitHub. Please use the following command for a smooth installation.
Install PDFGen via GitHub
Git clone https://github.com/AndreRenaud/PDFGen.git
You can also install it manually; download the latest release files directly from GitHub repository.
Create Basic PDF File via C API
PDFGen is an open-source, lightweight C library designed specifically for generating and managing PDF documents with ease. it supports various features for working with PDF documents, such as inserting text and images, adding new pages, manage fonts, and many more. The following example shows how software developers can create a single-page PDF with a title and a body of text inside C applications.
How to Create a Simple PDF via C Library?
#include
#include "pdfgen.h"
int main(void) {
// 1. Create a new PDF document with A4 page size
struct pdf_info info = {
.creator = "My App",
.producer = "My App",
.title = "My First PDF",
.author = "John Doe",
};
struct pdf_doc *pdf = pdf_create(PDF_A4_WIDTH, PDF_A4_HEIGHT, &info);
// 2. Set up a font
const int font_size = 20;
// 3. Add a new page
pdf_append_page(pdf);
// 6. Save the PDF to a file
pdf_save(pdf, "hello_world.pdf");
// 7. Free the allocated memory
pdf_destroy(pdf);
printf("PDF generated successfully!\n");
return 0;
}
PDF Text Manipulation via C Library
The open source PDFGen library has included complete support handling text inside PDF documents via C++ library. The library provides robust text handling capabilities, allowing you to use standard PDF fonts like Times-Roman, Helvetica, and Courier and control text appearance by setting the font size, color, and rotation of the text. The library also supports adding text at specific coordinates on the page. The following example shows how to add Text inside a PDF document using C commands.
How to Add Text inside a PDF File via C Library?
#include "pdfgen.h"
int main(void) {
struct pdf_info info = {
.creator = "My Application",
.title = "My First PDF",
.author = "Me"
};
struct pdf_doc *pdf = pdf_create(PDF_A4_WIDTH, PDF_A4_HEIGHT, &info);
pdf_set_font(pdf, "Times-Roman");
pdf_append_page(pdf);
pdf_add_text(pdf, NULL, "Hello, PDFGen!", 12, 50, 20, PDF_BLACK);
pdf_save(pdf, "hello.pdf");
pdf_destroy(pdf);
return 0;
}
Drawing Shapes and Embed Images in PDFs
The open source PDFGen library has provided powerful support for drawing shapes and adding images inside a PDF documents with ease. For documents that require more than just text, PDFGen offers a suite of functions for drawing basic shapes and lines. With just a couple of lines of code software developers can draw lines, rectangles, and circles to create shapes, charts, or borders inside their own applications. The following example demonstrates how software developers can draw graphics and add a JPEG image inside C Apps.
How to Draw Shapes inside PDF Documents via C Library?
#include "pdfgen.h"
int main(void) {
// ... (PDF creation boilerplate as above) ...
pdf_append_page(pdf);
// Draw a red line
pdf_add_line(pdf, NULL, 50, 50, 200, 50, 3, PDF_RED);
// Draw a blue-filled rectangle
pdf_add_filled_rect(pdf, NULL, 50, 100, 150, 50, PDF_BLUE);
// ... (Save and destroy PDF) ...
return 0;
}
Barcode Generation inside PDF via C Library
A standout feature of PDFGen is its built-in support for generating barcodes inside PDF documents. This is particularly useful for applications that need to produce invoices, shipping labels, or inventory tags. The library supports two common barcode standards. Code-128, which is a high-density linear barcode symbology and Code-39 which is a variable-length, discrete barcode symbology. Here is a useful example that shows how to add a Barcode to a PDF file inside C applications.
How to Add a Barcode inside PDF File inside C Apps?
#include "pdfgen.h"
int main(void) {
// ... (PDF creation boilerplate) ...
pdf_append_page(pdf);
pdf_add_barcode(pdf, NULL, PDF_BARCODE_128, 50, 200, 200, 50, "PDFGen-12345", PDF_BLACK);
// ... (Save and destroy PDF) ...
return 0;
}