I am using below code to convert docx to pdf using DOCX4j.While running this code into IDE it is working fine and space preserved after conversion. For deployment I created war and put it into tomcat server. But into test server space is not preserved into document after conversion.
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-JAXB-Internal -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-JAXB-ReferenceImpl -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-JAXB-MOXy -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-export-fo -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.7</version>
</dependency>
code for conversion
private byte[] docxToPdfBytes() {
InputStream templateInputStream = null;
try {
templateInputStream = new FileInputStream(ApplicationConstants.TEMP_DOCX_PATH);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
FileOutputStream os = new FileOutputStream(ApplicationConstants.TEMP_PDF_PATH);
Docx4J.toPDF(wordMLPackage, os);
os.flush();
os.close();
return Files.readAllBytes(Paths.get(ApplicationConstants.TEMP_PDF_PATH));
} catch (Throwable e) {
logger.info(XrefSubscriberServiceService.class.getName() + "==> Method : docxToPdfBytes");
logger.error(e.getMessage(), e);
}
finally {
if(null != templateInputStream) {
try {
templateInputStream.close();
} catch (IOException e) {
logger.info(XrefSubscriberServiceService.class.getName() + "==> Method : docxToPdfBytes");
logger.error(e.getMessage(), e);
}
}
}
return null;
}
So can anyone have idea why this type of behaviour is happened ??
this question is resolved. Just need to correct docx styling of font in docx document.
Related
Below is a example section of my java 1.8 program. It appears to be failing when trying to authenticate. It goes through a number of authentication methods and then declares its run out.
I would like to see debug information from within the sshj library to help me determine what's failing :- username, key exchange or something else. I am familiar with log4j and I can put logging statements within my code, but I can't find an example (simple to follow) which shows me how to hook up log4j to sfl4j and then tell sshj to use the logger.
'''
SSHClient sshClient = new SSHClient();
try
{
String username = "testuser";
File privateKey = new File("/mykeys/keyname");
KeyProvider keys;
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
keys = sshClient.loadKeys(privateKey.getPath());
sshClient.connect("1.2.3.4", 22);
sshClient.authPublickey(username, keys);
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.put("./send/file1.xml", "file1.xml");
sshClient.close();
}
catch (UserAuthException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
catch (TransportException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
catch (IOException e)
{
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
'''
Adding
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
to pom.xml
and creating a log4j.proerties files did the trick for me:
# Define the root logger with appender file
log = ssh-test.log
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
I have over 300 tables which i want to export to CSV files. However i see gibberish data.
I send the tablename and the retrieve the data using jdbctemplate I can see the data but when i export I see gibberish values
sr1org.springframework.util.LinkedCaseInsensitiveMapiEkMÑ=f$LcaseInsensitiveKeystLjava/util/HashMap
sr1org.springframework.util.LinkedCaseInsensitiveMapiEkMÑ=f$LcaseInsensitiveKeystLjava/util/HashMap
q~q~q~q~xq~sq~?#wq~sq~q~trivera92q~
Below is the code:
exportData.java
public List getTableData(String tableName){
return jdbcTemplate.queryForList("select * FROM " + tableName + " LIMIT 10");
}
To create csv file
#Autowired
ExportData exportData;
public void CreateCsvData(){
String tableName = "user_details";
String folderPath = "/tmp/Data/";
List data = this.exportData.getTableData(tableName);
try {
FileOutputStream fis = new FileOutputStream(folderPath + tableName + ".csv",true);
ObjectOutputStream os = new ObjectOutputStream(fis);
for (int i = 0; i < data.size(); i++) {
os.writeObject(data.get(i));
os.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
here is pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependencies>
I saw many examples with resultsetextractor and RowMapper but its difficult to write models for 300 tables.
Sorry my bad; I did iterate over the object forgot to paste it here;
I need to know how to convert a multipartFile to a XWPFDocument in order to read it as a word file ( the file that I uploaded is in fact a .docx ) however I receive the error :
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
Here is my code :
#Override
public void addReport(MultipartFile report) throws ApcException {
try{
File convFile = new File(report.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(report.getBytes());
fos.close();
FileInputStream fis = new FileInputStream(convFile.getAbsolutePath());
XWPFDocument docx = new XWPFDocument(fis);
List<XWPFTable> tables =docx.getTables();
invokeUpdate(ADD_REPORT,new Object[]{
tables.get(0).getRow(1).getCell(0),
tables.get(1).getRow(0).getCell(0),
tables.get(2).getRow(0).getCell(0),
tables.get(3).getRow(2).getCell(1),
tables.get(4).getRow(1).getCell(1)});
}
catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here are my Apache Dependencies in my pom.xml :
For Apache POI i have these :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
Thank you for your help !
You need to replace your apache poi version from 3.7 to 3.17.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
The latest stable release is Apache POI 3.17
NPE while receving messages from Queue. ( Only when messages are present in Queue). I feel like there is an issue with de-serializing the messages.
java.lang.NullPointerException
at com.sun.jersey.api.client.ClientResponse.getResponseDate(ClientResponse.java:738)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusRestProxy.receiveMessage(ServiceBusRestProxy.java:288)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusRestProxy.receiveQueueMessage(ServiceBusRestProxy.java:225)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusExceptionProcessor.receiveQueueMessage(ServiceBusExceptionProcessor.java:142)
RECEIVE_AND_DELETE option deletes the messages and throws NPE.
All other operations like create queue , send messages etc working fine. Any thoughts one this ?
Code to receive message
public void receiveMessage(String queueName) {
try {
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
while (true) {
ReceiveQueueMessageResult resultQM
= service.receiveQueueMessage(queueName, opts);
BrokeredMessage message = resultQM.getValue();
if (message != null && message.getMessageId() != null) {
log.println("MessageID: " + message.getMessageId());
// Display the queue message.
log.print("From queue: ");
byte[] b = new byte[200];
String s = null;
int numRead = message.getBody().read(b);
while (-1 != numRead) {
s = new String(b);
s = s.trim();
System.out.print(s);
numRead = message.getBody().read(b);
}
log.println("");
log.println("Custom Property: "
+ message.getProperty("MyProperty"));
// Remove message from queue.
log.println("Deleting this message.");
//service.deleteMessage(message);
} else {
log.println("Finishing up - no more messages.");
break;
// Added to handle no more messages.
// Could instead wait for more messages to be added.
}
}
} catch (Exception e) {
log.print(e);
}
}
Even though it was throwing NPE the actual issue was with the missing jar files. Here is the list of jar files
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-compute</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-network</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-sql</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-storage</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-websites</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-media</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-serviceruntime</artifactId>
<version>0.9.7</version>
</dependency>
</dependencies>
Iam trying to read a pdf file the code is
try {
File fileConn = new File(filePath);
InputStream inp = new FileInputStream(fileConn);
PdfReader reader = new PdfReader(inp);
int pages = reader.getNumberOfPages();
System.out.println("Pages" + pages);
} catch (Exception e) {
//Handle Exception
}
But the method is throwing NOClassDefFoundError. What coukd be the possible reason
Did you added pdfbox and itextpdf to your classpath?
Try this, if you're using maven:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>