Archive

Posts Tagged ‘apache’

Merging pdf using Apache PDFBox

November 5th, 2013 No comments

I was working on a problem in which we wanted to merge multiple pdf. We were using itext for merging pdf but wanted to try out open source alternative. The logical choice was ApachePDF Box.
It is not that IText does not work or the license fees is high. The organisation was trying to save few extra bucks although I firmly believe that a programmer should be paid for his hard work.

Below is the code sample for pdf merging with apache pdfbox

public class PDFDocumentMergerService {

private static final Log LOG = LogFactory
.getLog(ItextDocumentMergerService.class);

public void concatPDFWithPDFBOX(
final List<InputStream> streamOfPDFFiles,
final OutputStream outputStream) {

try {

PDFMergerUtility mergePdf = new PDFMergerUtility();
for (InputStream inputDoc : streamOfPDFFiles) {

mergePdf.addSource(inputDoc);
}
mergePdf.setDestinationStream(outputStream);
mergePdf.mergeDocuments();

}
catch (IOException ioe) {
throw new DocumentMergeException("Pdf merge failed.", ioe);
}
catch (COSVisitorException ex) {
throw new DocumentMergeException("Pdf merge failed.", ex);
}
}

public byte[] mergeDocuments(final List<byte[]> documentContents) {

List<InputStream> inputStreams = new ArrayList<InputStream>(
documentContents.size());

for (byte[] content : documentContents) {
inputStreams.add(new ByteArrayInputStream(content));
}

ByteArrayOutputStream mergedStream = new ByteArrayOutputStream();

long startTime2 = System.currentTimeMillis();

this.concatPDFWithPDFBOX(inputStreams, mergedStream);

long endTime2 = System.currentTimeMillis();

ItextDocumentMergerService.LOG.info(" Time to create PDF with PDF Box "
+ (endTime2 - startTime2));

byte[] outputData = mergedStream.toByteArray();

return outputData;
}

}

The performance was excellent and comparable to itext.

Invalid command ‘Header’, perhaps misspelled or defined by a module not included in the server configuration

February 7th, 2011 No comments

If you are seeing this error, enable in httpd.conf

LoadModule headers_module modules/mod_headers.so

Categories: Programming / tutorials Tags:

Adding Access-Control-Allow-Origin to server for Cross domain scripting

February 4th, 2011 1 comment

Cross domain scripting would be such a big issue especially in year 2011, I never imagined. What I had envisioned to be a small problem turned out to be much bigger and complicated than I thought.

Let us go over the problem statement first

I have application 1 http://www.skill-guru.com making a json request to http://ww.geekevalaution.com.

While it works in IE(I was working with IE6) , it will fail in Firefox (I was using FF 3.6)

From Mozilla docs

Cross-site HTTP requests are HTTP requests for resources from a different domain than the domain of the resource making the request.  For instance, a resource loaded from Domain A (http://domaina.example) such as an HTML web page, makes a request for a resource on Domain B (http://domainb.foo), such as an image, using the img element (http://domainb.foo/image.jpg).  This occurs very commonly on the web today — pages load a number of resources in a cross-site manner, including CSS stylesheets, images and scripts, and other resources.

Or another example

http:/127.0.0.1:8080/myservice  making call to http:/127.0.0.1:8090/myservice2

will also be not allowed in FF because even on same server and different port number , it considers it cross domain scripting. Read more…

Configuring Tomcat with Apache

February 2nd, 2011 4 comments

In this post we will describe how to set up Apache web server with Tomcat.

We have used  Tomcat 6.0.29 and Apache 2.2.17 but the settings describe here will work for Tomcat 7 as well.

1. We will need to install mod_jk on apache

Mod_jk can be downloaded  from this location

http://www.fightrice.com/mirrors/apache//tomcat/tomcat-connectors/jk/binaries/win32/

2. Copy mod_jk-1.2.30-httpd-2.2.3.so to <Install Location>\Apache Software Foundation\Apache2.2\modules

3. Create a workers.properties in conf folder of apache

A sample workers.properties

workers.tomcat_home=/usr/local/jakarta-tomcat-4.1.18

# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
#
workers.java_home=/usr/lib/java2

# You should configure your environment slash… ps=\ on NT and / on UNIX
# and maybe something different elsewhere.
#
ps=/

# The workers that your plugins should create and work with
#
worker.list=worker1

#—— DEFAULT ajp13 WORKER DEFINITION ——————————
#———————————————————————
# Defining a worker named ajp13 and of type ajp13
# Note that the name and the type do not have to match.
#
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13

#—— CLASSPATH DEFINITION —————————————–
#———————————————————————
# Additional class path components.
#
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)tomcat.jar

# The JVM that we are about to use
#
# Unix – Sun VM or blackdown
worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)lib$(ps)i386$(ps)classic$(ps)libjvm.so

# Setting the place for the stdout and stderr of tomcat
#
worker.inprocess.stdout=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stdout
worker.inprocess.stderr=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stderr

4. Use Tomcat auto configure.

To be added at the end of your httpd.conf

Include $TOMCAT_HOME/conf/jk/mod_jk.conf-auto

5. Re start tomcat and re start Apche.

You will find two folders auto and jk created under TOMCAT_HOME/conf.

http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

You will also find this value inserted in conf.xml

<Listener className=”org.apache.jk.config.ApacheConfig” modJk=”<Your apache directory>/Apache2.2/modules/mod_jk-1.2.30-httpd-2.2.3.so” />

Common Issues: Read more…

Apache Pivot Presentation at NEJUG

May 20th, 2010 No comments

NEJUG on Thu, Jun 10, will feature Greg Brown presenting on Apache Pivot and we will be at Sun Microsystems Burlington , MA.

Presentation Overview:

Apache Pivot (http://pivot.apache.org) is a platform for building rich Internet applications (RIAs) in Java. It combines the enhanced productivity and usability features of a modern RIA toolkit with the robustness of the industry-standard Java platform. Pivot includes a number of features that significantly simplify development of modern GUI applications, including an XML markup language for declaring the structure of a user interface, data binding, animated effects and transitions, and Web services integration.

In this session, Pivot founders Greg Brown and Todd Volkert provide an overview of the Pivot platform and walk through the implementation of a simple but practical application that highlights key platform features and development best practices.

About the presenter:

Greg Brown is a senior software architect with 15 years of experience developing applications in both a services and R&D capacity. He is the founder and PMC Chair of Apache Pivot, an open source platform for building rich Internet applications in Java. He holds a bachelor?s degree in Computer Systems Engineering from the University of Massachusetts Amherst.

Categories: Programming / tutorials Tags: ,