Tuesday, March 15, 2016

A Simple Rest WebService using Flask

Hi,

I will be doing this PyCharms IDE Community Edition with Python 3.5 version.

First of all you need to install the Flask package.

If you are using CLI , then the command would be

pip install Flask

If you are using PyCharms , then go to File>Settings>Project>Project Interpreter >Click on + button >Search for Flask > Install the package

Once the package is installed, then create a new python file in your project folder. 
I am naming it ekAurRestFile.py

Step 1 : Import the libraries

from Flask import flask,request

Step 2 :  Paste the code . Explanation at :  http://flask.pocoo.org/docs/0.10/quickstart/#quickstart

app = Flask("Rest Sample")

@app.route('/')
def sample_root():
    return 'Welcome'

@app.route('/hello')
def sample_hello():
    if 'name' in request.args:
        return 'Hello ' + request.args['name']
    else:
        return 'Hello Ankit'
if __name__ == '__main__':
    app.run()


Step 3 : Run the server 

If using cli , then navigate to to directory where the python file is present and then type
python ekAurRestFile.py

By the default your app is hosted at http://localhost:5000/

On hitting this url in your broswer , you will see a return as Welcome.

Now try with http://localhost:5000/hello and http://localhost:5000/hello?name=Vijay Dinanath Chahuhan

See the difference.

Happy learning !

Sunday, June 7, 2015

Node JS Tutorial

Download nodejs from https://nodejs.org/download/
Select Windows Installer 64 bit

Once installed , open command prompt and get ready.

navigate to nodeLearn by writing cd nodeLearn in command prompt

write npm install express --save and press enter in command prompt
write npm install -g express-generator and press enter in command prompt

This will install the express framework using which we will be able to make a simple webapp

Write this command in command prompt

express nodeLearn

this should create a folder in the current folder you currently are in command prompt

navigate to nodeLearn by writing cd nodeLearn in command prompt

write npm install and hit enter in command prompt

once installed, we are ready to launch

write npm start

your nodejs server would start

open a browser and point http://localhost:3000/

you would see welcome to express screen. Voila ! You have done it.


Sunday, October 26, 2014

Make a PDF unfillable using Java


Create  a new Java project in your favorite IDE. In my case , I would be using Eclipse.

Name your Java project - pdftest.
Right click on your project in the workspace and select new-->Folder.
Name it src.
Right click on src folder and select new--> Package. Name it com.ankit.pdf  .
Right click on the new package created and select new--> Class . Name it main.java .

We would be needing these three jars itextpdf-5.5.2.jar, bcpkix-jdk15on-1.47.jar, bcprov-jdk15on-1.47.jar .

I have used the code base from the article given here on iTextpdf's website  : http://itextpdf.com/examples/iia.php?id=219

This is just for learning purposes, I have no intention to use the codebase for any commercial purposes.

Code :

package com.ankit.pdf;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;


public class main {
    /** User password. */
    public static byte[] USER = "Hello".getBytes();
    /** Owner password. */
    public static byte[] OWNER = "World".getBytes();

// Please changes the values of RESULT1,RESULT2, RESULT3,RESULT4 while executing code in your machine.

    /** The resulting PDF file. */
    public static final String RESULT1
        = "F:/chapter12/encryption.pdf";
    /** The resulting PDF file. */
    public static final String RESULT2
        = "F:/chapter12/encryption_decrypted.pdf";
    /** The resulting PDF file. */
    public static final String RESULT3
        = "F:/chapter12/encryption_encrypted.pdf";
    public static final String RESULT4
    = "F:/sampleinp.pdf";

    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws DocumentException
     * @throws IOException
     */
    public void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
        writer.createXmpMetadata();
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        // step 5
        document.close();
    }

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void decryptPdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src, OWNER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        reader.close();
    }

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void encryptPdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.setEncryption(USER, OWNER,
            PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_MODIFY_CONTENTS, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);

// Setting setFormFlattening as true helps in making PDF form unfillable

        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
     
    }

    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {
        main2 metadata = new main2();
        metadata.createPdf(RESULT1);
        metadata.decryptPdf(RESULT1, RESULT2);
        metadata.encryptPdf(RESULT4, RESULT3);
    }
}


Please download the sample pdf form which is in fillable state originally which is used in variable RESULT4 from here : Link
To run, right click on main.java file and run as java application.


Please note : iText PDF software is not free and you need to purchase a license to use it.

I hope this small code snippet was useful to you. Let me know in the comments if it worked for you.



 
Tweet