Click here to Skip to main content
11,704,107 members (51,373 online)
Click here to Skip to main content

Simplest PDF Generating API for JPEG Image Content

, 19 Dec 2008 CPOL 55.8K 2.3K 45
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Hao Hu
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralHow to display image in original size Pin
Denny Mathew18-Dec-14 1:26
memberDenny Mathew18-Dec-14 1:26 
Questionwhy you only supports less than 256 Images? Pin
veeta23-Nov-14 20:20
memberveeta23-Nov-14 20:20 
QuestionExcellent Pin
T800G27-Sep-14 10:57
memberT800G27-Sep-14 10:57 
Questionpatches done: keep images's aspect ratio & automatic page rotate for best fit Pin
dab1826-Aug-11 6:55
memberdab1826-Aug-11 6:55 
QuestionRemove the max 250 pages limitation? Pin
monday200015-Mar-11 1:54
membermonday200015-Mar-11 1:54 
AnswerRe: Remove the max 250 pages limitation? Pin
Hao Hu15-Mar-11 7:15
memberHao Hu15-Mar-11 7:15 
QuestionHow to get DPI from JPEG? Pin
monday200011-Mar-11 5:22
membermonday200011-Mar-11 5:22 
AnswerRe: How to get DPI from JPEG? Pin
Hao Hu11-Mar-11 5:39
memberHao Hu11-Mar-11 5:39 
GeneralRe: How to get DPI from JPEG? Pin
monday200011-Mar-11 10:09
membermonday200011-Mar-11 10:09 
General Pin
Hao Hu26-Aug-10 12:27
memberHao Hu26-Aug-10 12:27 
GeneralRe: Pin
monday200012-Mar-11 21:20
membermonday200012-Mar-11 21:20 
QuestionWhat about the XMP manipulation? [modified] Pin
monday200017-Aug-10 23:11
membermonday200017-Aug-10 23:11 
AnswerRe: What about the XMP manipulation? Pin
Hao Hu18-Aug-10 6:09
memberHao Hu18-Aug-10 6:09 
GeneralDesired enhancements Pin
monday200014-Aug-10 0:09
membermonday200014-Aug-10 0:09 
GeneralRe: Desired enhancements Pin
Hao Hu14-Aug-10 15:12
memberHao Hu14-Aug-10 15:12 
Thanks a lot for your enthusiastic about this small library.

The original purpose of the library is to provide a start point of anyone who need a simple library code like this. Feel free to change it and use it at your own risk.

Regarding some of your concerns:

Computers always have trade-off. As an example, UNIX timestamps also have limitation of the possible time range. The trade-off of changing 256 to a unlimited number is the simplicity of the program. For my original motivation for writing this piece of code, 256 is enough for me. So I just want to keep the code simple.

Regarding 72DPI, I'm not 100% sure. But I think this is the predefined value in PDF spec. So I think changing this number to something else will only give yourself trouble.

Memory buffer size is also a trade-off technique for coding simplicity. I think you might not be able to find a case that will cause buffer overflow, since all the estimated sizes are safe enough. To give you an example, 2.45m is the current Olympic record for men high jump. So 3m will be a safe enough number to say no human can jump over. By using this kind of safe estimation, you might waste a little bit of memory, but you don't need to do dynamic memory allocation, which will spend extra time and also need add a lot of error checking logic.

For the paper size, I think even you use floating point number, but PDF actually will only use the integer value. You can easily verify this by opening any A4 document that generated by other PDF engine. I guess the reason is very simple, the error will only be 0.5*25.4/72=0.176mm, which is not significant at all.

Of course, there are many PDF libraries, they might provide more features that the code I have here, if you need those features, you should consider to switch.
GeneralRe: Desired enhancements Pin
monday200015-Aug-10 19:44
membermonday200015-Aug-10 19:44 
GeneralI made my program based on this one Pin
monday200013-Aug-10 1:12
membermonday200013-Aug-10 1:12 
GeneralRe: I made my program based on this one Pin
monday200016-Mar-11 22:03
membermonday200016-Mar-11 22:03 
GeneralRe: I made my program based on this one Pin
Hao Hu18-Mar-11 20:16
memberHao Hu18-Mar-11 20:16 
GeneralBug found Pin
monday200013-Aug-10 1:08
membermonday200013-Aug-10 1:08 
GeneralRe: Bug found [modified] Pin
Hao Hu13-Aug-10 5:43
memberHao Hu13-Aug-10 5:43 
QuestionMay I re-use your code under the "GPL 2 and later" license? Pin
monday20002-Jul-10 1:32
membermonday20002-Jul-10 1:32 
AnswerRe: May I re-use your code under the "GPL 2 and later" license? Pin
Hao Hu2-Jul-10 7:33
memberHao Hu2-Jul-10 7:33 
GeneralMy vote of 5 Pin
monday20001-Jul-10 23:06
membermonday20001-Jul-10 23:06 
GeneralBmp 2 pdf function Pin
Junson_Feng13-May-10 20:32
memberJunson_Feng13-May-10 20:32 
GeneralRe: Bmp 2 pdf function Pin
Hao Hu13-May-10 21:20
memberHao Hu13-May-10 21:20 
QuestionHow to keep image's aspect ratio? Pin
Super Garrison6-Nov-09 17:50
memberSuper Garrison6-Nov-09 17:50 
AnswerRe: How to keep image's aspect ratio? Pin
Hao Hu6-Nov-09 20:27
memberHao Hu6-Nov-09 20:27 
QuestionHow to add more than 2 images? Pin
WaleedH11-Jul-09 4:54
memberWaleedH11-Jul-09 4:54 
AnswerRe: How to add more than 2 images? Pin
Hao Hu11-Jul-09 13:03
memberHao Hu11-Jul-09 13:03 
GeneralRe: How to add more than 2 images? [modified] Pin
WaleedH11-Jul-09 19:58
memberWaleedH11-Jul-09 19:58 
GeneralRe: How to add more than 2 images? Pin
Hao Hu11-Jul-09 20:33
memberHao Hu11-Jul-09 20:33 
GeneralRe: How to add more than 2 images? Pin
WaleedH11-Jul-09 21:30
memberWaleedH11-Jul-09 21:30 
GeneralRe: How to add more than 2 images? Pin
Hao Hu11-Jul-09 21:48
memberHao Hu11-Jul-09 21:48 
Generalhi Pin
xyang_200928-May-09 14:55
memberxyang_200928-May-09 14:55 
GeneralRe: hi Pin
Hao Hu28-May-09 15:17
memberHao Hu28-May-09 15:17 
Generalvery good! Pin
ganfeng200320-Dec-08 16:14
memberganfeng200320-Dec-08 16:14 
GeneralRe: very good! Pin
Hao Hu20-Dec-08 22:01
memberHao Hu20-Dec-08 22:01 
GeneralJPEG Requirements Pin
Davide Zaccanti4-Oct-08 21:14
memberDavide Zaccanti4-Oct-08 21:14 
GeneralRe: JPEG Requirements Pin
Hao Hu4-Oct-08 21:32
memberHao Hu4-Oct-08 21:32 
GeneralRe: JPEG Requirements [modified] Pin
Davide Zaccanti4-Oct-08 23:22
memberDavide Zaccanti4-Oct-08 23:22 
GeneralRe: JPEG Requirements Pin
Hao Hu5-Oct-08 10:25
memberHao Hu5-Oct-08 10:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 19 Dec 2008
Article Copyright 2008 by Hao Hu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid