Send output to browser

I am converting one excel document to pdf document with dynamic data. Currently my input and output files are present on local system. I want to display the output pdf on new tab so that I can deploy on server. How to do that. Please give an example.

Hi,


Thanks for your posting and considering Aspose.Cells.

Sending output pdf to browser is not related to Aspose.Cells. However, you can convert your excel into pdf using Aspose.Cells APIs. Aspose.Cells allows you to save your output pdf directly to file path like c:/temp/out.pdf or to byte array output stream. It will give you output pdf in array of bytes i.e. byte[]. You should search on internet or stackoverflow how to send your array of bytes to your browser.

Please check the following sample code and its comments and the output pdf.

Java
Workbook wb = new Workbook();

Worksheet ws = wb.getWorksheets().get(0);
Cell c = ws.getCells().get(“A1”);
c.putValue(“Welcome to Aspose!”);

ws.autoFitColumn(0);

//Save excel to pdf on filepath
wb.save(“D:/Downloads/output.pdf”, SaveFormat.PDF);

//Save to byte array output stream
ByteArrayOutputStream baout = new ByteArrayOutputStream();
wb.save(baout, SaveFormat.PDF);

//Get the byte[] in which output pdf is saved
byte[] btsPdf = baout.toByteArray();

//Now i am sending byte[] to FileOutputStream to check it byte[] does contain output pdf
//You shoud send it browser (search on internet or stackoverflow how to send your bytes to browser)
FileOutputStream fout = new FileOutputStream(“D:/Downloads/output1.pdf”);
fout.write(btsPdf);
fout.flush();

Hello,

I have tried with trial version of aspose.
I have deploy aspose jar and fonts in my project dev server. Right now I am giving complete path of excel and pdf file in my program. I have also used FontConfig to give the font location folder path.

The performance in dev server is very bad I am getting. My excel having japanese font and it is taking around one minute to generate pdf document. Please suggest me what could be the issue and how to resolve it.

Hi,


Thanks for your posting and using Aspose.Cells.

Please share your excel file with us, we will convert it to pdf and see how much time it takes at our end. Sometime, files are big and they take lots of time. And sometime, excel file is just few KBs but when you convert it to pdf, it takes huge amount of time. And sometime, you want to print entire worksheet into one pdf page and worksheet is very big and it takes huge amount of time.

As an example, please check the sample.xlsx, it is just 8KB but it has 40 million pages (as shown in the Microsoft Excel Print Preview), so if you will convert it to Pdf, it will take huge amount of time.

Please check attached excel file.

Hi,


Thanks for your posting and using Aspose.Cells.

I have looked into your issue and found, there are 5 fonts used in your excel file. One of them is not available at my system which is highlighted in red color i.e. CorpoS. Please provide all of these fonts or at least the red one so that we could investigate this issue further.

  • MS Pゴシック
  • Arial
  • CorpoS
  • MS ゴシック
  • Cambria

Please check the attached folder for fonts files.

Currently, I have given complete path of my file location present in server and local disk (i.e /home/devsupt/aspose/PDF36_CoverSheet.xlsx). I want to give abousoute path like </test/abc.xls> for both xls file and pdf file. Is any specific syntax for this in aspose.

Hi,


Thanks for your posting and using Aspose.Cells.

If you want to give relative path, then first find your current directory and then append your relative path to it and it will work fine.

Please see the following sample code. The file abc.xlsx is present inside the current directory like /test/abc.xlsx. The output pdf is also created like /test/abc.pdf.

Java
File f = new File("");
System.out.println(f.getAbsolutePath());

String dirPath = f.getAbsolutePath();

Workbook wb = new Workbook(dirPath + “/test/abc.xlsx”);
wb.save(dirPath + “/test/abc.pdf”);

Console Output
Current Directory Path: D:\Aspose\Projects\Java\eclipse-workspace\PrjAspose

Hi,


Thanks for using Aspose.Cells.

Here is the code that will set your font directory which is present as /fonts/FontsUpdated/ and load your sample excel file which is present as /test/PDF36_Cover+Sheet.xlsx and generates the output file as /test/output.pdf.

I have also attached the output pdf file generated by the code for your reference.

Java
File f = new File("");
System.out.println(f.getAbsolutePath());

String dirPath = f.getAbsolutePath();

String fontFolder = dirPath + “/fonts/FontsUpdated/”;

FontConfigs.setFontFolder(fontFolder, true);

Workbook wb = new Workbook(dirPath + “/test/PDF36_Cover+Sheet.xlsx”);
wb.save(dirPath + “/test/output.pdf”);

Hi, Thanks for the input. In my case absolute path is different from what is coming user.dir. I want to ask one more thing here suppose I mention some path of pdf file in server. And if two users wants to access the pdf with their own dynamic data then what will happen. Because they are referring to same pdf in disk location. Or will be two threads for this. Let me know Thanks.

Hi,

Thanks for considering Aspose.Cells.

This question is related to computer science multi-threading concepts. You can search for its answers on the internet or stack over flow website.

Think of ASP.NET (for Windows Platform) and JSP (for Java based platforms), these are server side scripting languages. Whenever server gets a request from client, they create a separate thread which is sometime called Worker Process or Worker Thread. So, if you send 10 requests to servers like IIS, Apache, GlashFish, they will create 10 Worker Processes or Worker Threads. It means, Web Servers or Application Servers manage multi-threading by themselves and you do not need to worry about any multi-threading problems. Since, Aspose.Cells resides in the Worker Thread, so there is no issue of interruption or race conditions or deadlocks etc.

Issues appear only, when you start managing multi-threading yourself. In such cases, you must not share Workbook object in your multiple threads and each thread should have its own Workbook object. But if you want to share, then you will have to access shared resources via Semaphores or Mutex etc.

Suppose, your threads or other application threads are accessing the same object present on disk. Then, each of them should use Semaphore or Mutex so that no dead locks occurred. But this is necessary only when you are trying to write on disk. But if you are only reading the disk (by disk I mean your pdf file), then you do not need to worry at all. All of your applications or threads can read your pdf present at some location without any problem. But for writing, you must use multi-threading computer science concepts.

I have few more questions. Right now I am saving the generated output pdf file on disk (physical location) and then convert it to byte array to show it on new tab on browser. Is there is any way by which I need not to save on disk and directly show on new tab. Or if how do we delete it from disk. I have tried workbook.dispose() but it didn’t worked for me. Please let me know.

Hi,


Thanks for your posting and using Aspose.Cells.

I think, you are making a web application in Java. So you might be using Servlets or JSP or any other Java Web Application technology.

All these technologies can write your bytes in Web Browser directly. You don’t need to save them on disk and delete the files.

You can save your Workbook into Pdf in byte array instead on disk. Now, you will get a byte[] which contains your pdf. Your next job is to send this byte[] to web browser.

If you are working in JSP or Servlet, then you can use the following code to send your byte[] to your browser.

// The response from your servlet.
HttpServletResponse resp…
resp.setContentType(“application/pdf”);
resp.setHeader(“Content-Disposition”, “attachment;filename=output.pdf”);
resp.getOutputStream().write(byteArray);

Now your byte[] i.e. (byteArray) will be displayed in Web Browser as attachment or some browser may also open the pdf directly file.

Please also see the following post for sample code. It tells how to send Workbook object to Web Browser in XLSX format. In case of yours, you will use the above code that sends in Pdf format.

Thanks for the input. I am already using trial version. My organization applied for license and get a temporary license. What is the difference between trial version and temporary licence. Is there any performance improvement by this.

Hi,


Thanks for your posting and considering Aspose.Cells.

If you do not set license of Aspose.Cells, then it is called Trial Version of Aspose.Cells.

If you set a license of Aspose.Cells, then it is called Licensed Version of Aspose.Cells.

If you purchase a license then your license will not expired and Aspose.Cells will work in Licensed Version mode. When license is set, then two restrictions are lifted from Aspose.Cells.

1 - Aspose.Cells do not create Evaluation Warning in worksheets or images or pdf etc.
2 - Trial version can only open 100 excel workbooks in one thread. Licensed version does not have this restriction.

Temporary License is a license file which will expire after 30-Days. So when you will set it, Aspose.Cells will work in Licensed Version mode but after 30-Days, it will again start showing warnings.

Reference Links:


Hi,


Our organization has applied for aspose license. I have used the following file to include license file in code.
license.setLicense(“Aspose.Cells.lic”);
It is working fine in local but when I deployed the changes to server output pdf is not coming and it is not showing error as well in logs.
Is there is any limitation.

Hi,


Thanks for your posting and using Aspose.Cells.

First know, what is your current directory. Suppose your current directory is C:\abc\def or /uuu/ww/rr.

Then place your license file in your current directory. Now your license file path will be like

C:\abc\def\Aspose.Cells.lic (Windows)
/uuu/ww/rr/Aspose.Cells.lic (Linux)

Now, Aspose.Cells will automatically find the license. But if it still could not find the license file, then please set it like

license.setLicense(“C:\abc\def\Aspose.Cells.lic”); (Windows)
license.setLicense("/uuu/ww/rr/Aspose.Cells.lic"); (Linux)


Hi,


Thanks for the reply. Our organization has purchased aspose cell permanent license. I am storing output in byte array and showing it on new tab. I know there is one method to combine various work book. Is there is a way by which I can merge PDF file with my workbook. Please let me know.

Hi,


Thanks for your posting and purchasing Aspose.Cells.

Aspose.Cells can only convert your excel file into pdf. But it cannot combine two or more pdfs into one. For this purpose, you will need Aspose.Pdf. Please check the following links.