Save to PDF like Excel print-preview

What is the way to save a workbook to PDF with PDF pages being exactly the excel print-preview pages ?

Hi Sébastien,


Thank you for contacting Aspose support.

If you wish to save the Workbook to PDF as per Excel’s Print Preview then use the Workbook.save method with appropriate SaveFormat constant, that is; PDF. Please check the simplest code snippet as follow.

Java

new Workbook(dir + “book1.xlsx”).save(dir + “output.pdf”, SaveFormat.PDF);

It’s not what I get with my document.

See here same process saving as XLSX and as PDF.
PDF pages are splitted at columns that do not match the print-areas.

Hi again,


You are currently using Aspose.Cells for Java 8.6.3 that has rendered 6 PDF pages for your provided spreadsheet, that is incorrect. There should be total 3 pages, that is; 1 page for worksheet Cover & 2 pages for worksheet Data. I have tested the scenario against Aspose.Cells for Java 8.7.0.2 (latest at the moment) and I am getting correct results (attached). Please give a try to the latest release on your end as well.

I’ve got the same error with 8.7.0.2.

Edit: Apparently, the conversion error occurs on a Linux server, I cannot reproduce it on Windows.

Hi,

Thanks for your posting and using Aspose.Cells.

Actually your excel file needs font Calibri which is present in Windows. But when you convert your excel file to Pdf, this font is not present there and Aspose.Cells uses some different font instead.

You should get the Calibri font from your Windows machine inside some directory say “/usr/share/temp” and then set the font directory like

CellsHelper.setFontDir(“/usr/share/temp”);

before the beginning of the workbook creation. Then you will get the correct pdf. I have also attached the two screenshots showing your fonts in Windows and Linux.

Please also see the following article for your reference.

( How to Specify TrueType Fonts Location|Documentation )

At the beginning of my application, I call now : CellsHelper.setFontDirs()
In one of , I’ve dropped the TTF files taken from windows : CALIBRI.TTF, CALIBRIB.TTF, CALIBRII.TTF, CALIBRIL.TTF, CALIBRILI.TTF, CALIBRIZ.TTF

Wihtout this call, I had : WithoutFontDirs.jpg
The font is similar in appearance but not Calibri.

With the above setting, I obtain : WithFontDirs.jpg
The font is completely different and still not Calibri.

Edit 1 :
Just before saving, calling CellsHelper.getFontDirs() returns:
{
/usr/ipbx//lib/override/dev/reports,
/usr/ipbx//lib/override/reports,
/usr/ipbx//reports,
c:\windows\fonts,
c:\winnt\fonts,
/usr/share/fonts,
/usr/local/share/fonts,
/Library/Fonts,
/usr/java/jdk1.7.0_51/jre/lib/fonts
}
Here the 3 first paths are the ones I’ve set and the second one contains the Calibri TTF files.

Edit 2:
I’ve added a WarningCallback as documented HERE and I’m getting a bunch of traces concerning Calibri font substitution like these:
WARNING INFO: Font substitution: Font [ Calibri; Regular ] has been substituted in Cell [ C47 ] in Sheet [ Cover ].
WARNING INFO: Font substitution: Font [ Calibri; Bold ] has been substituted in Cell [ A1 ] in Sheet [ Data ].

Hi Sébastien,


Thank you for providing additional details.

The presented problem looks related to missing fonts as you are getting the font substitution warnings as well as correct font (Calibri) is not getting embedded in your generated PDF files. Furthermore, I was able to generate correct results while using the latest revision of the API (8.7.0.2) by placing & setting the Calibri TTFs in CentOS 6.5. Please check the attached resultant PDF file.

Please note, I am using the CellsHelper.setFontDir as opposed to CellsHelper.setFontDirs method. Could you please give the following piece of code a try by amending the directory locations? Moreover, please place the fonts in a folder which is accessible to your user, such as Downloads, for testing purposes.

Java

String dir = “/home/admin/Downloads/”;
String infile = dir + “FILE.xlsx”;
String outfile = dir + “file.pdf”;

CellsHelper.setFontDir(dir + “calibri”); //calibri is the folder containing TTFs of Calibri font

License li = new License();
li.setLicense(dir + “Aspose.Total.Java.lic”);

new Workbook(infile).save(outfile, SaveFormat.PDF);

Hi again,


This is to update you that I have reevaluate the case by using the CellsHelper.setFontDirs method as demonstrated below, and the result is correct in this case as well.

Java

ArrayList list = new ArrayList();
list.add(dir + “calibri”);
CellsHelper.setFontDirs(list);

Please try moving the font setting statements before the license setting statement. Moreover, verify that the folder is accessible to your user/application where you have placed the TTFs because the Aspose.Cells APIs will not cause any exception if a given folder is not accessible or the API does not find any fonts there. However, callback for font substitution will give you idea on what's going on behind the scene. Another important point to note is that if you are using the JVM arguments -DAspose.Cells.FontDirExc=, it will force the API to look for fonts in specified directory only where all other font directories (even explicitly specified) will be ignored.
babar.raza:
Please try moving the font setting statements before the license setting statement.


This is the information I needed to make it work, thanks !

It is somehow strange to have to do it before loading the license, by the way.
Hi,

It is good to know that you are able to get desired results.

centile:

It is somehow strange to have to do it before loading the license, by the way.

Regarding your concerns as quoted above, it is advised to set the font location before initializing any class from Aspose.Cells APIs. However, while using the latest APIs, it is not necessary to set the font directory before setting the license as I am able to get correct results by moving the license setting statements before the CellsHelper.setFontDir(s) method. If you are still using your older revision of the API, please test this case against the latest release (8.7.0.2).