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

Simplest PDF Generating API for JPEG Image Content

By , 19 Dec 2008
 

Introduction

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:

PJPEG2PDF pPDF;
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 */
        Jpeg2PDF_AddJpeg(pPDF, JPEG_IMGW, JPEG_IMGH, JPEG_BYTE_SIZE, 
		JPEG_DATA_POINTER, IS_COLOR_JPEG);
    } 
    
    /* 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.

History

  • 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.

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralBug foundmembermonday200013 Aug '10 - 1:08 
Hello, Hao Hu.
 
I found a bug in your program.
 
Inside the function
 
STATUS Jpeg2PDF_GetFinalDocumentAndCleanup(PJPEG2PDF pPDF, UINT8 *outPDF, UINT32 *outPDFSize)
 
you should comment the statement:
 
if(outPDF && (*outPDFSize >= pPDF->currentOffSet))
 
To understand why, initialize *outPDFSize with zero (in the calling function) - and your program is to stop working (at least on the big-filesize 24 bit JPEGs).
 
It currently relies on the randomly non-zero initialized *outPDFSize - which works in Debug, but does not in Release (on the big-filesize 24 bit JPEGs).
GeneralRe: Bug found [modified]memberHao Hu13 Aug '10 - 5:43 
Thanks for your message.
 
However, I think the place you point out is not right. The issue is within the test code.
Basically, when calling Jpeg2PDF_GetFinalDocumentAndCleanup(), I want user to pass in
their buffer size to avoid the situation of overflow. So the *outPDFSize should be initialized
in the caller as the size of the outPDF buffer. Later, I'll change that value and let caller
know the exact byte size that has been used. This is a very common way for API to get then set
a buffer size.
 
It seems complicate for me to update this article right now. (codeproject ask to submit my change to their editor for the update, which is much more complicate than before)
 
So what should been done is in the testMain.c:
Just remove the declaration of: pdfFinalSize and replace all pdfFinalSize with pdfSize.
So pdfSize will be a In/Out variable.
In: Caller let the jpeg2pdf know the byte size of pdfBuf
Out: jpeg2pdf let the caller know the actual byte size that has been used.
 
Thanks.

modified on Saturday, August 14, 2010 4:26 AM

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

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