How to export pdf data with dynamic Object in openpdf java

375 views Asked by At

I'm trying to use openpdf for dynamic field.

Maven:

<dependency>
    <groupId>com.github.librepdf</groupId>
    <artifactId>openpdf</artifactId>
    <version>1.3.8</version>
</dependency>

Find fields: (dynamic find field names)

Type type = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) type;
Class<V> entity = (Class<V>) pt.getActualTypeArguments()[0];

// create Header
List<String> columnNames = new ArrayList<>();

Field[] superFields = entity.getSuperclass().getDeclaredFields();
for (Field field : superFields) {
    if ( ! field.getName().equals("serialVersionUID"))
        columnNames.add( field.getName() );
}

Field[] fields = entity.getDeclaredFields();
for (Field field : fields) {
    if ( ! field.getName().equals("serialVersionUID"))
        columnNames.add( field.getName() );
}

for (String columnName : columnNames) {
    cell.setPhrase( new Phrase(columnName));
    table.addCell(cell);
}

document.add(table);
document.close();

How to change dynamic this? (I use this code for only User object)

List<User> users = repository.findAll();
for (User user : Users) {
    cell.setPhrase( new Phrase( user.getId() ));
    cell.setPhrase( new Phrase( user.getFirstName() ));
    cell.setPhrase( new Phrase( user.getLastName() ));
}

Is there a better library for PDF export?

Thanks

1

There are 1 answers

0
mehnet ali On BEST ANSWER

I write some code for that problem:

first I can get list of name methods:

private final MethodCache cache = new MethodCache();

public List<Object> toArray(Object source, String[] nameMapping) throws Exception {

        extractBeanValues(source, nameMapping);

        return beanValues;
    }
    
    private final MethodCache cache = new MethodCache();
    private final List<Object> beanValues = new ArrayList<>();
    
    private void extractBeanValues(Object source, String[] nameMapping) throws Exception {
        if (source == null)
            throw new NullPointerException("the bean to write should not be null");
        if (nameMapping == null) {
            throw new NullPointerException(
                    "the nameMapping array can't be null as it's used to map from fields to columns");
        }

        this.beanValues.clear();

        for (int i = 0; i < nameMapping.length; i++) {
            String fieldName = nameMapping[i];

            if (fieldName == null) {
                this.beanValues.add(null);
            } else {
                Method getMethod = this.cache.getGetMethod(source, fieldName);
                try {
                    this.beanValues.add(getMethod.invoke(source, new Object[0]));
                } catch (Exception e) {
                    throw new Exception(
                            String.format("error extracting bean value for field %s", new Object[] { fieldName }), e);
                }
            }
        }
    }

and second convert to List<Object> :

private final List<Object> beanValues = new ArrayList<>();

public List<Object> toArray(Object source, String[] nameMapping) throws Exception {

    extractBeanValues(source, nameMapping);
    return beanValues;
}

and the end :

List<V> list = repository.findAll();
for (V v: list ) {
    toArray(v, names);

    for (Object obj: this.beanValues) {
       cell.setPhrase( new Phrase( obj.toString() ) );
       table.addCell(cell);
    }
}

now I can call the method with dynamic Object and field