Sunday, May 29, 2011

Thoughts about power

The problem with power is that... well... it's POWERFUL.  It can be used for both good and ill, but regardless of it's usage, the more power there is behind a command, the harder it is for the individual to resist.  That's pretty close to the definition of power.

It's become clear that throughout history there are some people who are driven to accumulate power.  It's also become clear that there is no correlation between a person's ability to accumulate power and his/her desire to use it to benefit mankind.  Certainly there have been examples of power being used very wisely, and for the benefit of man -- but there more than numerous regrettable examples.

The Libertarian viewpoint is that the only way to prevent these tragedies is to prevent the accumulation of power.  We believe that all power originates from the people as individuals, thus governments and corporations only acquire power by a lessening of the power of the people.  If government's sole purpose becomes to protect civil rights and to preserve the power of the people above government and corporations, government immediately becomes more honest.  If government no longer has the ability to help corporations make laws against the people, corporations will have no reason to try to influence government.

I think that man has a natural inclination to accumulate power, and power positively reinforces the people who accumulate it.  It seems to me (and most libertarians) that the only way to stop people from accumulating power is to cut off the source of the power.  That can only be done by refusing to allow people to yield their rights, and refusing to allow governments or corporations to curtail the rights of the people.

Will this hinder our ability to do great good deeds and achieve mighty milestones?  Somewhat.  People can still come together in free associations.  Polio was defeated by just such an organization when the March of Dimes rallied the country and found a cure.

Will it also stop the misappropriation of power?  I believe so.  And that is a benefit that is worthwhile.

Wednesday, May 18, 2011

Concatenating BIRT reports via servlet

I've been using BIRT a bit and in our project we generate alot of PDFs and have found BIRT to be the beswt product there is for that.  One thing BIRT does NOT allow you to do is to combine reports.  The below is my Servlet based solution for that which correctly renumbers the pages  It borrows the concatenation from Abhi on Java.

I also bought IText in Action  A must have for the serious developer of PDF's in Java.

I've stripped out the company specific part of this code, but have not tried compiling this version.  it should be pretty damn close to what you need though.  Ask if you have any questions.

Of course this code is completely free to use and is released under the Academic Free License


import javax.servlet.*;
import javax.servlet.http.*;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;

import java.util.*;

public class PDFServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("application/pdf"); // Code 1
       List<InputStream> pdfs = new ArrayList<InputStream>();
       String prefix=http://myBirtServer?__report=report/";
       String params="&oid="+request.getParameter("oid")+"&__format=pdf";
       String [] URLs={
       for(int x=0;x<URLs.length;x++){
         URL u1=new URL(prefix+URLs[x]+params);
         InputStream in=u1.openStream();


* Method written by Abhi
public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate, String watermark) {
        Document document = new Document(PageSize.LETTER.rotate());
        try {
            List<InputStream> pdfs = streamOfPDFFiles;
            List<PdfReader> readers = new ArrayList<PdfReader>();
            int totalPages = 0;
            Iterator<InputStream> iteratorPDFs = pdfs.iterator();
            int x = 0;
            // Create Readers for the pdfs.
            while (iteratorPDFs.hasNext()) {
                System.out.println((++x) + " through the loop1");
                try {
                    InputStream pdf =;
                    PdfReader pdfReader = new PdfReader(pdf);
                    totalPages += pdfReader.getNumberOfPages();
                } catch (Exception e) {
            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            if (!STR.isEmpty(watermark)) {
                writer.setPageEvent(new Watermark(watermark));
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
            // data

            PdfImportedPage page;
            int currentPageNumber = 0;
            int pageOfCurrentReaderPDF = 0;
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();

            // Loop through the PDF files and add to the output.
            while (iteratorPDFReader.hasNext()) {
                PdfReader pdfReader =;

                // Create a new page in the target for each source page.
                while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                    page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                    cb.addTemplate(page, 0, 0);

                    // Code for pagination.
                    if (paginate) {
                        cb.setFontAndSize(bf, 9);
                        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0);
                pageOfCurrentReaderPDF = 0;
        } catch (Exception e) {
        } finally {
            if (document.isOpen())
            try {
                if (outputStream != null)
            } catch (IOException ioe) {

     * Inner class to add a watermark to every page.
    static class Watermark extends PdfPageEventHelper {

        private String watermark;

        public Watermark(String wm) {
            watermark = wm;

        Font FONT = new Font(Font.FontFamily.HELVETICA, 52, Font.BOLD, new GrayColor(0.75f));

        public void onEndPage(PdfWriter writer, Document document) {
                    Element.ALIGN_CENTER, new Phrase(watermark, FONT),
                    421, 297.5f, writer.getPageNumber() % 2 == 1 ? 45 : -45);