Click here to Skip to main content
15,499,289 members
Articles / Programming Languages / C
Posted 3 Oct 2008


48 bookmarked

Simplest PDF Generating API for JPEG Image Content

Rate me:
Please Sign up or sign in to vote.
4.43/5 (19 votes)
19 Dec 2008CPOL1 min read
An article on how to generate PDF file based on JPEG file as page content


I was working on a project in which I need to wrap a JPEG file into PDF format. The program needs to be done in C, and after searched on the Internet, I could not find anything that I can refer to. Most of the Open Source PDF engine is based on either Java or PHP, and a few C PDF engines are huge and will add a lot of unnecessary code to my project. I decided to write this simple JPEG to PDF wrapper. And it's the result of reverse-engineering of the simplest PDF file that contains one single JPEG file. I just want to share this API so you can grab and use it if you have a similar requirement.

Using the Code

Just give an example to demonstrate how to generate a 2 page PDF file based on 2 JPEG files. Please refer to testMain.c for details. But the idea is:

int pdfByteSize, pdfOutByteSize;
unsigned char *pdfBuf;

pPDF = Jpeg2PDF_BeginDocument(8.5, 11);    
	/* pdfW, pdfH: Page Size in Inch ( 1 inch=25.4 mm ); Letter Size 8.5x11 */

if(NULL != pPDF) {
    Loop For All JPEG Files {
        ... Prepare the current JPEG File to be inserted.
        /* You'll need to know the dimension of the JPEG Image, 
	and the ByteSize of the JPEG Image */
    /* Call this after all of the JPEG image has been inserted. 
	The return value is the PDF file Byte Size */
    pdfByteSize = Jpeg2PDF_EndDocument(pPDF);
    /* Allocate the buffer for PDF Output */
    pdfBuf = malloc(pdfByteSize);
    /* Output the PDF to the pdfBuf */
   Jpeg2PDF_GetFinalDocumentAndCleanup(pPDF, pdfBuf, &pdfOutByteSize);
   ... Do something you want to the PDF file in the memory.

There are several places that you can fine-tune in the Jpeg2PDF.h file:

#define MAX_PDF_PAGES        256     /* Currently only supports less than 256 Images */

#define PDF_TOP_MARGIN        (0.0 * PDF_DOT_PER_INCH)    /* Currently No Top Margin */
#define PDF_LEFT_MARGIN        (0.0 * PDF_DOT_PER_INCH)   /* Currently No Left Margin */

That's it, guys. Enjoy.


  • Updated [2008-12-19] - Added some extra code to auto scan the current folder and automatically obtain the JPEG image dimension from the JPEG file instead of using hard coded value before.
    The JPEG image dimension code is borrowed from here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

AnswerRe: May I re-use your code under the "GPL 2 and later" license? Pin
Hao Hu2-Jul-10 8:33
MemberHao Hu2-Jul-10 8:33 
GeneralMy vote of 5 Pin
monday20002-Jul-10 0:06
Membermonday20002-Jul-10 0:06 
GeneralBmp 2 pdf function Pin
Junson_Feng13-May-10 21:32
MemberJunson_Feng13-May-10 21:32 
GeneralRe: Bmp 2 pdf function Pin
Hao Hu13-May-10 22:20
MemberHao Hu13-May-10 22:20 
QuestionHow to keep image's aspect ratio? Pin
Super Garrison6-Nov-09 18:50
MemberSuper Garrison6-Nov-09 18:50 
AnswerRe: How to keep image's aspect ratio? Pin
Hao Hu6-Nov-09 21:27
MemberHao Hu6-Nov-09 21:27 
QuestionHow to add more than 2 images? Pin
WaleedH11-Jul-09 5:54
MemberWaleedH11-Jul-09 5:54 
AnswerRe: How to add more than 2 images? Pin
Hao Hu11-Jul-09 14:03
MemberHao Hu11-Jul-09 14:03 
GeneralRe: How to add more than 2 images? [modified] Pin
WaleedH11-Jul-09 20:58
MemberWaleedH11-Jul-09 20:58 
GeneralRe: How to add more than 2 images? Pin
Hao Hu11-Jul-09 21:33
MemberHao Hu11-Jul-09 21:33 
GeneralRe: How to add more than 2 images? Pin
WaleedH11-Jul-09 22:30
MemberWaleedH11-Jul-09 22:30 
GeneralRe: How to add more than 2 images? Pin
Hao Hu11-Jul-09 22:48
MemberHao Hu11-Jul-09 22:48 
Generalhi Pin
xyang_200928-May-09 15:55
Memberxyang_200928-May-09 15:55 
GeneralRe: hi Pin
Hao Hu28-May-09 16:17
MemberHao Hu28-May-09 16:17 
Generalvery good! Pin
ganfeng200320-Dec-08 17:14
Memberganfeng200320-Dec-08 17:14 
GeneralRe: very good! Pin
Hao Hu20-Dec-08 23:01
MemberHao Hu20-Dec-08 23:01 
GeneralJPEG Requirements Pin
Davide Zaccanti4-Oct-08 22:14
MemberDavide Zaccanti4-Oct-08 22:14 
GeneralRe: JPEG Requirements Pin
Hao Hu4-Oct-08 22:32
MemberHao Hu4-Oct-08 22:32 
GeneralRe: JPEG Requirements [modified] Pin
Davide Zaccanti5-Oct-08 0:22
MemberDavide Zaccanti5-Oct-08 0:22 
GeneralRe: JPEG Requirements Pin
Hao Hu5-Oct-08 11:25
MemberHao Hu5-Oct-08 11:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.