Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:10:33+00:00 2026-06-15T16:10:33+00:00

I am planning to use, excel to pdf api (REST) in my java code

  • 0

I am planning to use, excel to pdf api (REST) in my java code provided by ConvertApi.
please share the code snippet to be used for the same.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:10:34+00:00Added an answer on June 15, 2026 at 4:10 pm

    Please try the code below

    Library

    package com.excel2pdfconvert.example;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.DefaultHttpClient;
    
    public class Xls2PDFConvertBean {
    
        private File file;
        private String outputDir;
        
        
        private String filesize;
        private String filename;
        
        
        
        public void setOutputDir(String outputDir) {
            this.outputDir = outputDir;
        }
        public void setExcelFile(File file) {
            this.file = file;
        }
        
        public String getFilesize() {
            return filesize;
        }
        public String getFilename() {
            return filename;
        }
        
        
        /**
         * Run request for page conversion
         * @return resultcode "0" in error, "1" in success
         * @throws UnsupportedEncodingException 
         */
        public String doRequest() throws UnsupportedEncodingException{
            
            String resultcode = "0"; 
            
            HttpPost httppost = new HttpPost("https://v2.convertapi.com/convert/xlsx/to/pdf");
            
            MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
            
            // For File parameters
            entity.addPart("file", new FileBody(file, "binary/octet-stream"));
            
            httppost.setEntity( entity );
            
            HttpClient httpclient = new DefaultHttpClient();
            try {
                HttpResponse response = httpclient.execute(httppost);
                Header rcHeader = response.getFirstHeader("result");
                if(rcHeader != null){
                    
                    resultcode = rcHeader.getValue();
                    
                    if("True".equals(resultcode)){
                        
                        filesize = response.getFirstHeader("filesize").getValue();
                        filename = response.getFirstHeader("OutputFileName").getValue();
                        
                        HttpEntity hentity = response.getEntity();
                        if(hentity != null){
    
                            InputStream istream = hentity.getContent();
                            File file = new File(outputDir+File.separator+filename);
                            FileOutputStream ostream = new FileOutputStream(file);
                            
                            byte[] b = new byte[1024];
                            int num = 0;
                            while( (num = istream.read(b, 0, b.length)) > 0)
                                ostream.write(b, 0, num);
                            istream.close();
                            ostream.flush();
                            ostream.close();
                            
                        }
                    }
                }
                
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            return resultcode;
        }
        
    }
    

    The web page

    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%@ page import="com.excel2pdfconvert.example.Xls2PDFConvertBean" %>
    <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
    <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
    <%@ page import="java.util.*" %>
    <%@ page import="java.io.File" %>
    <%@ page import="org.apache.commons.fileupload.FileItem" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>
        Excel2Pdf Api Demo
    </title>
        <script type="text/javascript">
            function BeforeConvert() {
                document.getElementById('LabelMessage').innerHTML = 'Please wait...';
                document.getElementById('HyperLinkFile').innerHTML = '';
                document.getElementById('LabelFileSize').innerHTML = '';
                document.getElementById('LabelFileName').innerHTML = '';
            }
        </script> 
    </head>
    <body>
    
    <form method="post" enctype="multipart/form-data">
    
        <div>
            <h1>Excel2Pdf Api Demo</h1>
            Upload Excel file:&nbsp;
            <input type="file" name="excel" />&nbsp;
            <input type="submit" name="btnConvert" value="Convert" onclick="BeforeConvert();" id="btnConvert" />
            <br />
            <br />
            
            
    <% if(ServletFileUpload.isMultipartContent(request)){
        
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = upload.parseRequest(request);
        
        File uploadedFile = null;
        String[] allowedExt = {"csv", "xls", "xlsx", "xlsb", "xlt", "xltx"};
        
        // Process the uploaded items
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
    
            if (! item.isFormField()) {
                 
                //Check for valid excel extensions
                String fileName = item.getName();
                boolean isValid = false;
                 for(String ext : allowedExt){
                     if(fileName.contains(ext)){
                        isValid = true;
                        break;
                     }
                 }
                 
                 if(isValid){
                     uploadedFile = new File(application.getRealPath("/") + "/" + fileName);
                     item.write(uploadedFile);
                 }
            }
        }
    
        if(uploadedFile == null){
            out.println("<div>Please upload file in following format: csv, xls, xlsx, xlsb, xlt, xltx");
        }else{
            
            Xls2PDFConvertBean xls2pdf = new Xls2PDFConvertBean();
            xls2pdf.setOutputDir(application.getRealPath("/"));
            xls2pdf.setExcelFile(uploadedFile);
            String resultcode = xls2pdf.doRequest();
            
            //Remove uploaded file after conversion
            uploadedFile.delete();
            
            if(resultcode == null || "False".equals(resultcode)){
                out.println("<div>Can not convert file</div>");
            }else{
    %>
            <span id="LabelMessage">Conversion successful  </span>
            <br />
            <a id="HyperLinkFile" href="<%= xls2pdf.getFilename() %>" >Click here to open file  </a>
    
            <br />
            <span id="LabelFileSize">File size: <%= xls2pdf.getFilesize() %></span>
            <br />
            <span id="LabelFileName">File name: <%= xls2pdf.getFilename() %></span>
     <%
                }
            }
        }else{
    %>
        <span id="LabelMessage"></span>
    <%      
        }
    %>
    
    
        </div>
    </form>
       
    </body>
    
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im planning to use java/jsp for web application installed on virtual web hosting space
I am planning to use XPath to query an XML file. Can you please
Was planning to use Service Routing (on WCF/REST) to do some common tasks before
I am planning to use the PayPal Adaptive Payments API. I want to do
Planning to use Object tag to display pdf file inline in HTML file. Is
I am planning to use http://www.objis.com/formationextjs/lib/extjs-4.0.0/docs/api/Ext.data.Store.html to store a table and filter the data...
I am planning to use .NET Client along with a Java Server. Are there
I'm planning to use this piece of code in my Asp.net app string strUserInputtedHashedPassword
We are planning to use foursquare api to get venue in a particular city.
I am planning to use Silverlight control for bing map. It will be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.