Search This Blog

Showing posts with label to. Show all posts
Showing posts with label to. Show all posts

Monday, February 21, 2022

How to enable JavaScript in your Chrome browser for localhost

 

Google Chrome

  1. On the web browser menu click on the "Customize and control Google Chrome" and select "Settings".
  2. In the "Settings" section click on the "Show advanced settings..."
  3. Under the the "Privacy" click on the "Content settings...".
  4. When the dialog window opens, look for the "JavaScript" section and select "Allow all sites to run JavaScript (recommended)".
  5. Click on the "OK" button to close it.
  6. Close the "Settings" tab.
  7. Click on the "Reload this page" button of the web browser to refresh the page.


Wednesday, June 30, 2021

Python - Image to Pdf

1. Install img2pdf

> pip/pip3 install img2pdf

2. img2pdfown.py


# importing necessary libraries

import img2pdf

from PIL import Image

import os

  

# CLI  img2pdf *jpg -o output_all_pngs.pdf

'''

img_path = "image1.jpg";

pdf_path = "output.pdf";

image = Image.open(img_path);

pdf_bytes = img2pdf.convert(image.filename);

file = open(pdf_path, "wb");

file.write(pdf_bytes);

image.close();

file.close();

'''


# convert all files ending in .jpg inside a directory

dirname = "dump/images/"

imgs = []

#dpix = dpiy = 300;

#layout_fun = img2pdf.get_fixed_dpi_layout_fun((dpix, dpiy))

for fname in os.listdir(dirname):

    if not fname.endswith(".jpg"):

        continue

    path = os.path.join(dirname, fname)

    if os.path.isdir(path):

        continue

    imgs.append(path)

    print ("\n" + path);

with open("dump/output.pdf", "wb") as f:

    #f.write(img2pdf.convert(imgs, layout_fun=layout_fun));

    f.write(img2pdf.convert(imgs));

  

'''    

imgs = ['dump/images/page_1.jpg', 'dump/images/page_2.jpg', 'dump/images/page_10.jpg']

with open("dump/output1.pdf", "wb") as f:

    f.write(img2pdf.convert(imgs))


# specify paper size (A4)

a4inpt = (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297))

layout_fun = img2pdf.get_layout_fun(a4inpt)

with open("name.pdf", "wb") as f:

    f.write(img2pdf.convert('test.jpg', layout_fun=layout_fun))


# use a fixed dpi of 300 instead of reading it from the image

dpix = dpiy = 300;

layout_fun = img2pdf.get_fixed_dpi_layout_fun((dpix, dpiy))

with open("name.pdf", "wb") as f:

    f.write(img2pdf.convert('test.jpg', layout_fun=layout_fun))


# create a PDF/A-1b compliant document by passing an ICC profile

with open("name.pdf", "wb") as f:

    f.write(img2pdf.convert('test.jpg', pdfa="/usr/share/color/icc/sRGB.icc"))    

'''

    


Wednesday, November 27, 2019

Flying Saucer HTML to PDF

Add this maven

<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->

<dependency>

    <groupId>org.xhtmlrenderer</groupId>

    <artifactId>flying-saucer-pdf-itext5</artifactId>

    <version>9.1.19</version>

</dependency>


Code 

        String inputFile = "d:/xxx.html";
        String url = new File(inputFile).toURI().toURL().toString();
        String outputFile = "d:/xxx.pdf";
        OutputStream os = new FileOutputStream(outputFile);
       
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);
        renderer.layout();
        renderer.createPDF(os);
       

        os.close();


Note
It will throw if any " QName production: QName::=(NCName':')?NCName. ' Exception in thread "main" org.xhtmlrenderer.util.XRRuntimeException: "  Then your html tag has problem. correct that html tag and re run it.

for ex: <Table width: 50%"> 
     to
      <table width="50%">

Friday, September 28, 2018

Nodejs code to convert string date to another date format.

Sample nodejs code to convert string date to another format.


date.js

var moment = require('moment');
bar ('1980-08-29', 'YYYY-MM-DD', 'DD/MM/YYYY');    //calling method

function bar(date, currentFormat, requiredFormat )
{
    var date = moment(date,currentFormat);
    var train_date = date.format(requiredFormat);
    console.log(train_date);        // 29/08/1980
    return train_date;
}
exports.bar = bar;



//npm install --save
//node date.js

Tuesday, November 8, 2016

How to fetch from list of nested list- Dynamic Loop

Assume the bean is like below, CommentsBean has CommentsBeanlist.

public class CommentsBean {

int id;
int parentId;
String msg;
List commentsBeanList;

public CommentsBean() {

}

public CommentsBean(int id, int parentId, String msg) {
super();
this.id = id;
this.parentId = parentId;
this.msg = msg;
}

@Override
public String toString() {
return "CommentsBean [id=" + id + ", parentId=" + parentId + ", msg=" + msg + ", commentsBeanList="
+ commentsBeanList + "]";
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getParentId() {
return parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public List getCommentsBeanList() {
return commentsBeanList;
}

public void setCommentsBeanList(List commentsBeanList) {
this.commentsBeanList = commentsBeanList;
}


}


Program to extract that dynamically.

public class DynamicListLoop {

public static void main(String[] args) {
List commntsBeanList = new ArrayList<>();
List commntsBeanChildList1 = new ArrayList<>();
List commntsBeanChildList11 = new ArrayList<>();
CommentsBean commentsBeanchild20 = new CommentsBean(4,-1, "second");
CommentsBean commentsBeanchild21 = new CommentsBean(5,4, "second-child-1");
commntsBeanChildList11.add(commentsBeanchild20);
commntsBeanChildList11.add(commentsBeanchild21);
CommentsBean commentsBeanchild2 = new CommentsBean(3,2, "first-child-2");
CommentsBean commentsBeanchild = new CommentsBean(2,1, "first-child-1");
commentsBeanchild.setCommentsBeanList(commntsBeanChildList11);
commntsBeanChildList1.add(commentsBeanchild);
commntsBeanChildList1.add(commentsBeanchild2);
CommentsBean commentsBean = new CommentsBean(1,-1, "first");
commentsBean.setCommentsBeanList(commntsBeanChildList1);
commntsBeanList.add(commentsBean);
System.out.println(commntsBeanList.toString());
//write a function and pass the parent bean list.
commentsBeanParseInLoop (commntsBeanList );

}
public static void commentsBeanParseInLoop ( List commntsBeanList )
{
if ( commntsBeanList!=null && commntsBeanList.size() > 0 )
{
for ( CommentsBean cb : commntsBeanList )
{
System.out.println(cb.getId() + " : " + cb.getMsg() );
if ( cb.getCommentsBeanList() !=null && cb.getCommentsBeanList().size() > 0 )
{
//call the same function and pass the inner bean list
commentsBeanParseInLoop ( cb.getCommentsBeanList() );
}
}
}
}

}

Hit Counter


View My Stats