Skip to content

Instantly share code, notes, and snippets.

@rurounijones
Created February 27, 2012 00:14
Show Gist options
  • Save rurounijones/1920005 to your computer and use it in GitHub Desktop.
Save rurounijones/1920005 to your computer and use it in GitHub Desktop.
Datalogics C++ Example
/* Copyright 2007 Datalogics, Inc. All rights reserved.
This program tests the PDF2IMG C API functionality for reading PDFs
from blocks of memory and writing PDFs to blocks of memory. Posted got this Gist with permission
*/
#include "pdf2imglib.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(UNIX_ENV)
#include <ctype.h>
#endif
int main(int argc, char *argv[])
{
ImageConversion iC = 0;
int retVal = 0,
pdf2imgErrVal = 0;
char *inputName = "input.pdf";
FILE *inputFile = 0;
unsigned char *picBuf = 0;
unsigned int picBufSize = 0;
if (argc >= 2)
inputName = argv[1];
if (!(inputFile = fopen(inputName, "rb")) )
{
printf("Could not open %s; halting.\n", inputName);
return 1;
}
fseek(inputFile, 0, SEEK_END);
picBufSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
if (!(picBuf = new unsigned char[picBufSize]) )
{
printf("Could not allocate %d bytes for input; halting.\n", picBufSize);
fclose(inputFile);
return 1;
}
if (picBufSize != fread(picBuf, 1, picBufSize, inputFile))
{
printf("Could not read %s; halting.\n", inputName);
fclose(inputFile);
delete(picBuf);
return 1;
}
fclose(inputFile);
if (pdf2img_init(NULL, 0))
{
printf("Error initializing pdf2img; halting.\n");
return 1;
}
iC = pdf2img_new_memconversion(picBuf, picBufSize);
if ((0 == iC) || (0 > pdf2img_get_num_pages(iC)) )
{
printf("Error opening %s from memory; halting\n", inputName);
pdf2img_term();
return -2;
}
printf("Opened %s (%d pages) from memory block. Converting first page...\n",
inputName, pdf2img_get_num_pages(iC));
pdf2img_set_output_type(iC, GIFoutput);
pdf2img_convert_page(iC, 1, NULL); // pass NULL to make a "memory" conversion
size_t memNeeded = pdf2img_get_pagememsize(iC);
if (memNeeded > 0)
{
void *picBuf = malloc(memNeeded);
if (picBuf)
{
FILE *outGr = 0;
outGr = fopen("out.gif", "wb");
pdf2img_get_pagemem(iC, picBuf, (unsigned int) memNeeded);
fwrite(picBuf, 1, memNeeded, outGr);
fclose(outGr);
free(picBuf);
pdf2img_release_pagemem(iC);
}
}
pdf2img_destroy_conversion(iC);
pdf2img_term();
return retVal;
}
require 'ffi'
# A Basic script to test FFI with the PDF2IMG library. See if we can get it
# working first then we will do all the nice encapsulation
#
# Tested on Ruby 1.9.3-p0 on Windows XP with the 32bit .dll files.
# Final target, JRuby (in 1.9 mode) on Windows 64bit with 32 or 64bit .dll files
module Pdf2Img
extend FFI::Library
ffi_lib "pdf2imglib.dll"
@@pointer_data = nil
# int pdf2img_init (const char *fontDirList[], unsigned int listLen)
attach_function :init, :pdf2img_init, [:string, :uint], :int
# ImageConversion pdf2img_new_memconversion (const void *inPDFMem, unsigned int numBytes)
attach_function :new_memconversion, :pdf2img_new_memconversion, [:pointer, :uint], :pointer
# int pdf2img_set_output_type (ImageConversion iC, OutputTypeCode oCode)
# typedef enum {
# EPSoutput,
# TIFFoutput,
# JPEGoutput,
# BMPoutput,
# PNGoutput,
# RAWoutput,
# PDFoutput,
# GIFoutput,
# } OutputTypeCode;
attach_function :set_output_type, :pdf2img_set_output_type, [:pointer, :int], :int
# int pdf2img_convert_page (ImageConversion iC, unsigned int pageNum, const char *outputPath)
# Note: Pass in NULL as the output path for an in-memory conversion
attach_function :convert_page, :pdf2img_convert_page, [:pointer, :uint, :string], :int
# size_t pdf2img_get_pagememsize (ImageConversion iC)
attach_function :get_pagememsize, :pdf2img_get_pagememsize, [:pointer], :size_t
# int pdf2img_get_pagemem (ImageConversion iC, void *memBuf, unsigned int bufCapacity)
attach_function :get_pagemem, :pdf2img_get_pagemem, [:pointer, :pointer, :uint], :int
def self.get_image(iC)
puts "Getting the amount of memory needed for the image data"
puts "iC Class is #{iC.class}, null: #{iC.null?}"
image_size = self.get_pagememsize(iC) # Line 70 of cpp file
puts "The amount of memory required is #{image_size / 1024 / 1024}MB"
# According to the above call the amount of memory required is 4095MB, What!?
# The C Program does this at this point
# void *picBuf = malloc(memNeeded);
# So we mallox the needed memory
picBuf = ::LibC.malloc(image_size)
raise "picBuf pointer is null" if picBuf.null?
if self.get_pagemem(iC, picBuf, image_size) == -1
raise "Get Image Data Exception"
end
raise "picBuf pointer is null" if picBuf.null?
# picBuf *should* now contain image data so lets get it
picBuf.get_bytes(0, image_size)
end
def self.pdf_data_to_pointer(data)
@@pointer_data = FFI::MemoryPointer.new(:char, data.size)
@@pointer_data.put_bytes(0, data)
end
def self.data
@@pointer_data
end
end
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
# call #attach_function to attach to malloc, free, memcpy, bcopy, etc.
attach_function :malloc, [:size_t], :pointer
attach_function :free, [:pointer], :void
end
puts "Reading in PDF file"
f = File.new(File.join("c:","Datalogics","PDF2IMG","test.pdf"),"rb")
pdf_data = f.read
puts "The size of the PDF data is #{(pdf_data.size / 1024.to_f)} KB"
# The size of the PDF data is 435.2109375 KB
# Create a pointer to the PDF data in the Pdf2Img module, again messy but we can clean this up later.
Pdf2Img.pdf_data_to_pointer(pdf_data)
puts "Initializing Pdf2Img"
if Pdf2Img.init(nil,0) == -1 # Line 53 of cpp file
raise "Initialization error"
end
puts "Creating memory conversion"
iC = Pdf2Img.new_memconversion(Pdf2Img.data, pdf_data.size) # Line 59 of cpp file
puts "Setting the output"
if Pdf2Img.set_output_type(iC, 2) == -1 # Line 68 of cpp file , we want a GIF output to match the cpp file so output type 7
raise "Set Output Exception"
end
puts "Converting the first page to an image"
if Pdf2Img.convert_page(iC, 1, nil) == -1 # Line 69 of cpp file
raise "Conversion Exception"
end
puts "Getting the image data"
image_data = Pdf2Img.get_image(iC)
File.open(File.join("c:","Datalogics","PDF2IMG","test.jpg"), 'wb') {|f| f.write(image_data) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment