Click here to Skip to main content
Click here to Skip to main content

Simplest PDF Generating API for JPEG Image Content

, 19 Dec 2008 CPOL
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

Comments and Discussions

GeneralHow to display image in original size PinmemberDenny Mathew18-Dec-14 2:26 
Questionwhy you only supports less than 256 Images? Pinmemberveeta23-Nov-14 21:20 
QuestionExcellent PinmemberT800G27-Sep-14 11:57 
Questionpatches done: keep images's aspect ratio & automatic page rotate for best fit Pinmemberdab1826-Aug-11 7:55 
QuestionRemove the max 250 pages limitation? Pinmembermonday200015-Mar-11 2:54 
AnswerRe: Remove the max 250 pages limitation? PinmemberHao Hu15-Mar-11 8:15 
QuestionHow to get DPI from JPEG? Pinmembermonday200011-Mar-11 6:22 
AnswerRe: How to get DPI from JPEG? PinmemberHao Hu11-Mar-11 6:39 
GeneralRe: How to get DPI from JPEG? Pinmembermonday200011-Mar-11 11:09 
General PinmemberHao Hu26-Aug-10 13:27 
GeneralRe: Pinmembermonday200012-Mar-11 22:20 
QuestionWhat about the XMP manipulation? [modified] Pinmembermonday200018-Aug-10 0:11 
AnswerRe: What about the XMP manipulation? PinmemberHao Hu18-Aug-10 7:09 
GeneralDesired enhancements Pinmembermonday200014-Aug-10 1:09 
Hello, Hao Hu.
I'm planning to enhance a bit your program.
First of all, it is required to remove the limitation of 256 pages. This might be done via the temp file usage - instead of the RAM memory usage. It is currently the main flaw - the max files amount shouldn't be limitated at all.
Also I'd like to consider the real files DPI instead of the hard-coded 72 dpi.
Besides it is not the good idea to use the memory buffers of a pre-set size - what is the guarantee that their length would be always enough?
And I used A4 paper size in my clone of your program. I write the canvas size to PDF as double (%.6f)- and you - as integer (%.d) (which is not precise). I am from Russia, and in Russia we do not use inches - instead we use only the millimeters.
Also in your program a small JPG is stretched to the whole caanvas. I will make an option to keep the JPG original size (placing it in the center of the canvas).
I found also some analoguous PDF library - But I don't know how to compile it in my MS VC++ 6.0 for Windows.
GeneralRe: Desired enhancements PinmemberHao Hu14-Aug-10 16:12 
GeneralRe: Desired enhancements Pinmembermonday200015-Aug-10 20:44 
GeneralI made my program based on this one Pinmembermonday200013-Aug-10 2:12 
GeneralRe: I made my program based on this one Pinmembermonday200016-Mar-11 23:03 
GeneralRe: I made my program based on this one PinmemberHao Hu18-Mar-11 21:16 
GeneralBug found Pinmembermonday200013-Aug-10 2:08 
GeneralRe: Bug found [modified] PinmemberHao Hu13-Aug-10 6:43 
QuestionMay I re-use your code under the "GPL 2 and later" license? Pinmembermonday20002-Jul-10 2:32 
AnswerRe: May I re-use your code under the "GPL 2 and later" license? PinmemberHao Hu2-Jul-10 8:33 
GeneralMy vote of 5 Pinmembermonday20002-Jul-10 0:06 
GeneralBmp 2 pdf function PinmemberJunson_Feng13-May-10 21:32 
GeneralRe: Bmp 2 pdf function PinmemberHao Hu13-May-10 22:20 
QuestionHow to keep image's aspect ratio? PinmemberSuper Garrison6-Nov-09 18:50 
AnswerRe: How to keep image's aspect ratio? PinmemberHao Hu6-Nov-09 21:27 
QuestionHow to add more than 2 images? PinmemberWaleedH11-Jul-09 5:54 
AnswerRe: How to add more than 2 images? PinmemberHao Hu11-Jul-09 14:03 
GeneralRe: How to add more than 2 images? [modified] PinmemberWaleedH11-Jul-09 20:58 
GeneralRe: How to add more than 2 images? PinmemberHao Hu11-Jul-09 21:33 
GeneralRe: How to add more than 2 images? PinmemberWaleedH11-Jul-09 22:30 
GeneralRe: How to add more than 2 images? PinmemberHao Hu11-Jul-09 22:48 
Generalhi Pinmemberxyang_200928-May-09 15:55 
GeneralRe: hi PinmemberHao Hu28-May-09 16:17 
Generalvery good! Pinmemberganfeng200320-Dec-08 17:14 
GeneralRe: very good! PinmemberHao Hu20-Dec-08 23:01 
GeneralJPEG Requirements PinmemberDavide Zaccanti4-Oct-08 22:14 
GeneralRe: JPEG Requirements PinmemberHao Hu4-Oct-08 22:32 
GeneralRe: JPEG Requirements [modified] PinmemberDavide Zaccanti5-Oct-08 0:22 
GeneralRe: JPEG Requirements PinmemberHao Hu5-Oct-08 11: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.141223.1 | Last Updated 19 Dec 2008
Article Copyright 2008 by Hao Hu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid