Generate PDF files in Java on the fly

September 29th, 2009No Comments  |  

iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won’t use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you’ll build iText into your own applications so that you can automate the PDF creation and manipulation process. For instance in one or more of the following situations:

* Due to time or size, the PDF documents can’t be produced manually.
* The content of the document must be calculated or based on user input.
* The content needs to be customized or personalized.
* The PDF content needs to be served in a web environment.
* Documents are to be created in “batch process” mode.

You can use iText to:

* Serve PDF to a browser
* Generate dynamic documents from XML files or databases
* Use PDF’s many interactive features
* Add bookmarks, page numbers, watermarks, etc.
* Split, concatenate, and manipulate PDF pages
* Automate filling out of PDF forms
* Add digital signatures to a PDF file
* And much more

The library is easy to use and can produce great results with only little programming. The basic usage is pretty simple. Let’s take a look at a small “Hello World” application:

Document document = new Document();
try {
    PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));

    document.open();
    document.add(new Paragraph("Hello World"));
} catch (Exception e) {

}
document.close();

Creating a new document object will initialize a new PDF document with A4 dimensions. The PdfWriter will be used to write the resulting document to an OutputStream, which is in the above example a file. After opening the document you can just add new paragraphs to the document.

org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn’t contain a multipart/form-data or multipart/mixed stream, content type header is null

If you are iterating the items from the multipart request without checking if an http request is encoded in multipart format
then you will get this error.

If ServletFileUpload.isMultipartContent(request) returns true then you need to get the fields via the upload.parseRequest(request) method.

eg:

// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart)
{
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();

// Set factory constraints
factory.setSizeThreshold(1000000);
factory.setRepository( ConfigServlet.getTempDirectory() );

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint
upload.setSizeMax(10000000);

// Parse the request
List items;
try {
items = upload.parseRequest(request);

for(FileItem item : items)
{

if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
query.put(name, value);

}
else // file object
{
// do stuff for file request

}

}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
query = new TreeMap();
Map tbl = request.getParameterMap();
query.putAll( tbl );
}

Postfix Header Checks

September 22nd, 20092 Comments  |  

Postfix header checks to reject spam mails :

/^Subject:.*=\?(big5|euc-kr|gb2312|ks_c_5601-1987)\?/ REJECT No foreign character sets, please.

/^(Content-Type:.*|\s+)charset\s*=\s*”?(big5|euc-kr|gb2312|koi8|Windows-1251|ks_c_5601-1987)”?/ REJECT No foreign character sets, please.

/^(Content-Type:.*|\s+)charset\s*=\s*”?(Windows-1251)\?/ REJECT Probably a windows virus.

/^From:.*\@.*\.r[uo]/ REJECT No Russian or Romanian mail, please.

/^From:.*\@.*\.bg/ REJECT No foreign character sets for bg, please.

/^From:.*\@.*\.nz/ REJECT No foreign character sets for nz, please.

/^From:.*\@.*\.lb/ REJECT No foreign character sets for lb, please.

/^From:.*\@.*\.cz/ REJECT No foreign character sets for cz, please.

/^From:.*\@.*\.kr/ REJECT No foreign character sets for kr, please.

/^From:(.*)Screen Saver / REJECT Screensaver virus, die.

/^From:(.*)mchsi.com/ REJECT Stop sending us crap.

/^From:lpinsk@localnet.com]/ REJECT Stop sending us crap.

/^From:(.*)[Aa]meritas/ REJECT Die, spammer, die.

/opt-in/ REJECT Typical Spammer Word “Opt..”

/^Received:.*\.{50,}/ REJECT Bogus received address

/^(Received|Message-Id|X-(Mailer|Sender)):.*\b(AutoMail|E-Broadcaster|Emailer Platinum|eMarksman|Extractor|e-Merge|
from stealth[^.]|Global Messenger|GroupMaster|Mailcast|MailKing|
Match10|MassE-Mail|massmail\.pl|News Breaker|Powermailer|Quick Shot|Ready Aim Fire|WindoZ|WorldMerge|Yourdora|Lite)\b/ REJECT You’re using a mass mailer, therefore you’re bounced.

/^received:.*xxxxxxxxxxxxxxxx/ REJECT X-Rated Subject Line

/^Received:(.*)hispeedmailer\.com/ REJECT Mass-mailer. Die, spammer, die.

/^From:(.*)cupid\@valentines-ecard\.com/ REJECT Potential Virus Content

/^Return-Path:(.*)offers@/ REJECT We get too many offers.

/^Subject:(.*)penis/ REJECT You said the p-word

/^Subject:(.*)your password/ REJECT Die, spammer, die.

/^Subject:(.*)Bigger Bust/ REJECT We’ve got big busts, already.

/^Subject:(.*)adult/ REJECT X-Rated Subject Line

/^Subject:(.*)AS SEEN ON T\.V/ REJECT We don’t watch TV.

/^Subject:(.*)barnyard/ REJECT No barnyard please

/^Subject:(.*)beast/ REJECT X-Rated Subject Line

/^Subject:(.*)bitch/ REJECT X-Rated Subject Line

/^Subject:(.*)bodies/ REJECT X-Rated Subject Line

/^Subject:(.*)breast/ REJECT X-Rated Subject Line

/^Subject:(.*)fuck/ REJECT X-Rated Subject Line

/^Subject:(.*)galore/ REJECT X-Rated Subject Line

/^Subject:(.*)gay/ REJECT X-Rated Subject Line

/^Subject:(.*)porn/ REJECT X-Rated Subject Line

/^Subject:(.*)slut/ REJECT X-Rated Subject Line

/^Subject:(.*)HARDCORE/ REJECT X-Rated Subject Line

/^Subject:(.*)russian/ REJECT God bless america

/^Subject:(.*)teen/ REJECT X-Rated Subject Line

/^Subject:(.*)viagra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)viaagra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)viiagra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)v1agra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)viagr@/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)v1@gra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)viag*ra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)soma/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)levitra/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)ambien/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)tramadol/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)phentermine/ REJECT No thanks, we’ve got plenty.

/^Subject:(.*)xxx/ REJECT X-Rated Subject Line

/^Subject:(.*)orgasms/ REJECT X-Rated Subject Line

/^Subject:(.*)size does matter/ REJECT X-Rated Subject Line

/^(To|From|Cc|Reply-To):.*@sexyfun.net/ REJECT X-Rated Subject Line

/^To:(.*)infomail@recurrent\.com/ REJECT Die, spammer, die.

/^(To|From|Cc|Reply-To):.*@sunny95.com/ REJECT We don’t like radio.

/^X-Mailer:.*\b(Aristotle|Avalanche|Blaster|Bomber|DejaVu|eMerge|Extractor|UltraMail|Sonic
|Floodgate|GeoList|Mach10|MegaPro|Aureate|MultiMailer|Bluecom|Achi-Kochi Mail|Direct Email|Andrew’s SuperCool Blastoise|MailerGear|Advanced Mass Sender|SpireMail|MailWorkZ|UltimDBMail|Mabry|Lite)\b/ REJECT No mass-mailers, please.

/^Subject:(.*)pen[i1]s/ REJECT You said the p-word

/^Subject:(.*)\bdick\b/ REJECT X-rated spam

/^(To|From|Cc|Reply-To):.*@sympatico.ca/ REJECT You spammed us, now you’re blocked. Sympatico.ca…

/^(To|From|Cc|Reply-To):.*@optonline/ REJECT You spammed us, now you’re blocked. Optonline…

/^(To|From|Cc|Reply-To):.*@sendspeed/ REJECT You spammed us, now you’re blocked. Sendspeed…

/^(To|From|Cc|Reply-To):.*@pillsavings/ REJECT You spammed us, now you’re blocked. pillsavings…

/^(To|From|Cc|Reply-To):.*@edirectmessage.com/ REJECT You spammed us, now you’re blocked. edirectmessage…

/(.*)?\{6,\}/ REJECT Too many ?????

/(.*)[X|x]\{3,\}/ REJECT Too many Xs

/^(To|From|Cc|Reply-To):.*@lists.itsahullabaloo-lists.net/ REJECT No more hullabalos, please

/^(To|From|Cc|Reply-To|Subject):.*Free Cellular Phone/ REJECT We already have cell phones.

/^(From|mailto):.*@sina.com/ REJECT sina.com spammed us, now you’re out.

/^(From|mailto):.*@bomx1.com/ REJECT bomx1.com spammed us, now you’re out.

/^(From|mailto):.*@pornopassprt.com/ REJECT no porn mail, please.

/^Subject:(.*)[Ss]ildenalfil/ REJECT You meant the v-word.

/^Subject:(.*)[Cc]itrate/ REJECT You meant the v-word.

/^Subject:(.*)[Cc]ialis/ REJECT c_ialis

/^Subject:(.*)[Ee]nzyte/ REJECT e_nzyte

/^Content\-Transfer\-Encoding:(.*)base64$/ REJECT (base_64)

download txt file