Convert HTML to PDF using Java | Font Crash on IBM i Series | Retain Table Cell width with colspan Attribute

Hi there,
Please see below the crash that I receive when trying to run a report using Aspose PDF 17.7

Exception in thread "main" class com.aspose.pdf.internal.ms.System.z93: Cannot find any fonts installed on the system.
com.aspose.pdf.internal.p431.z10.m1(Unknown Source)                                                                   
com.aspose.pdf.internal.p431.z10.m1(Unknown Source)                                                                   
com.aspose.pdf.internal.p431.z10.m1(Unknown Source)                                                                   
com.aspose.pdf.internal.p476.z2.m1(Unknown Source)                                                                    
com.aspose.pdf.internal.p475.z10.m1(Unknown Source)                                                                   
com.aspose.pdf.internal.p485.z4.m1(Unknown Source)                                                                    
com.aspose.pdf.internal.p443.z33.<init>(Unknown Source)                                                               
com.aspose.pdf.internal.p443.z5.m1(Unknown Source)                                                                    
com.aspose.pdf.internal.p443.z5.<init>(Unknown Source)                                                                
com.aspose.pdf.internal.p484.z21.m1(Unknown Source)                                                                   
com.aspose.pdf.internal.p452.z2.<init>(Unknown Source)                                                                
com.aspose.pdf.internal.p442.z3.m1(Unknown Source)                                                                    
com.aspose.pdf.internal.p522.z1.m1(Unknown Source)                                                                    
com.aspose.pdf.internal.p558.z1.m1(Unknown Source)                                                                    
com.aspose.pdf.internal.p521.z13.m1(Unknown Source)                                                                   
com.aspose.pdf.z90.m1(Unknown Source)   
com.aspose.pdf.z90.m1(Unknown Source)          
com.aspose.pdf.ADocument.m1(Unknown Source)    
com.aspose.pdf.ADocument.<init>(Unknown Source)
com.aspose.pdf.Document.<init>(Unknown Source)     
blu.utils.AsposeHtmlToPdf.main(AsposeHtmlToPdf.java:34)           
        at com.aspose.pdf.internal.p431.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p431.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p431.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p476.z2.m1(Unknown Source)     
        at com.aspose.pdf.internal.p475.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p485.z4.m1(Unknown Source)     
        at com.aspose.pdf.internal.p443.z33.<init>(Unknown Source)
        at com.aspose.pdf.internal.p443.z5.m1(Unknown Source)     
        at com.aspose.pdf.internal.p443.z5.<init>(Unknown Source) 
        at com.aspose.pdf.internal.p484.z21.m1(Unknown Source)    
        at com.aspose.pdf.internal.p452.z2.<init>(Unknown Source) 
        at com.aspose.pdf.internal.p442.z3.m1(Unknown Source)        
          at com.aspose.pdf.internal.p522.z1.m1(Unknown Source)     
          at com.aspose.pdf.internal.p558.z1.m1(Unknown Source)     
          at com.aspose.pdf.internal.p521.z13.m1(Unknown Source)    
          at com.aspose.pdf.z90.m1(Unknown Source)                  
          at com.aspose.pdf.z90.m1(Unknown Source)                  
          at com.aspose.pdf.ADocument.m1(Unknown Source)            
          at com.aspose.pdf.ADocument.<init>(Unknown Source)        
          at com.aspose.pdf.Document.<init>(Unknown Source)         
          at blu.utils.AsposeHtmlToPdf.main(AsposeHtmlToPdf.java:34)
  Java program completed with exit code 1                                                                                    

Please assist.

Thanks in Advance.

Regards,
Duane

@duanep026

Would you please make sure that all necessary fonts are installed in the system where you are processing PDF files with Aspose.PDF. Please try to install all Microsoft essential fonts in the system and if does not help, please share sample code snippet and complete environment details with us to reproduce the error e.g. OS Name and Version, Application Type, JDK Version, etc. We will test the scenario in our environment and address it accordingly.

Hi,

Please can I get some assistance on how to have all the correct fonts installed. My application runs fine locally on eclipse, it’s only when running on the as400 that this error comes up. I have attached my html and the pdf that I have converted to it and also my source code. Please also assist in correcting my pdf so the before and after versions are not different. The pdf has 2 differences to the html:

  1. The grid on the right does not align correctly on the pdf(you will see)
  2. The terms and conditions on the pdf runs over 2 lines where as it runs over 1 line on the html.

Thanks in advance for you assistance. Much appreciated.

Regards,
Duanepdf.zip (55.2 KB)

Hi Asad,
I think I have managed to fix the font error by using a custom folder. But I’m now getting the below error when running on the ibm I series as400:

Exception in thread "main" java.lang.NullPointerException        
        at com.aspose.pdf.internal.p63.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p63.z10.m2(Unknown Source)    
        at com.aspose.pdf.internal.p63.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p65.z4.m1(Unknown Source)     
        at com.aspose.pdf.internal.p65.z4.m1(Unknown Source)     
        at com.aspose.pdf.internal.p65.z4.m1(Unknown Source)     
        at com.aspose.pdf.internal.p66.z1.m4(Unknown Source)     
        at com.aspose.pdf.internal.p66.z7.<init>(Unknown Source) 
        at com.aspose.pdf.internal.p66.z1.<init>(Unknown Source) 
        at com.aspose.pdf.internal.p66.z3.<init>(Unknown Source) 
        at com.aspose.pdf.internal.p65.z10.m1(Unknown Source)    
        at com.aspose.pdf.internal.p68.z32.m30(Unknown Source)   
        at com.aspose.pdf.internal.p68.z38.m1(Unknown Source)    
        at com.aspose.pdf.internal.p68.z32.m1(Unknown Source)    
        at com.aspose.pdf.internal.p69.z1.m1(Unknown Source)     
        at com.aspose.pdf.internal.p70.z1.m1(Unknown Source)     
at com.aspose.pdf.internal.p70.z1.m1(Unknown Source)                      
at com.aspose.pdf.internal.p71.z6.m1(Unknown Source)                      
at com.aspose.pdf.internal.p71.z6.m1(Unknown Source)                      
at com.aspose.pdf.internal.p71.z6.m1(Unknown Source)                      
at com.aspose.pdf.TextState.setFont(Unknown Source)                       
at com.aspose.pdf.TextState.m1(Unknown Source)                            
at com.aspose.pdf.TextState.m1(Unknown Source)                            
at com.aspose.pdf.TextSegment.m1(Unknown Source)                          
at com.aspose.pdf.TextBuilder.m1(Unknown Source)                          
at com.aspose.pdf.TextBuilder.appendText(Unknown Source)                  
at com.aspose.pdf.z9.m1(Unknown Source)                                   
at com.aspose.pdf.internal.foundation.rendering.z17.accept(Unknown Source)
at com.aspose.pdf.internal.foundation.rendering.z14.accept(Unknown Source)
at com.aspose.pdf.internal.foundation.rendering.z11.accept(Unknown Source)
at com.aspose.pdf.internal.foundation.rendering.z14.accept(Unknown Source)
at com.aspose.pdf.internal.foundation.rendering.z11.accept(Unknown Source)
at com.aspose.pdf.internal.foundation.rendering.z14.accept(Unknown Source)
        at com.aspose.pdf.internal.foundation.rendering.z11.accept(Unknown Source)
        at com.aspose.pdf.internal.foundation.rendering.z14.accept(Unknown Source)
        at com.aspose.pdf.internal.foundation.rendering.z30.accept(Unknown Source)
        at com.aspose.pdf.z9.m1(Unknown Source)                                   
        at com.aspose.pdf.ApsUsingConverter.m1(Unknown Source)                    
        at com.aspose.pdf.z90.m1(Unknown Source)                                  
        at com.aspose.pdf.ADocument.m1(Unknown Source)                            
        at com.aspose.pdf.ADocument.<init>(Unknown Source)                        
        at com.aspose.pdf.Document.<init>(Unknown Source)                         
        at blu.utils.AsposeHtmlToPdf.main(AsposeHtmlToPdf.java:41)                
Java program completed with exit code 1              

My code I below:

//License for Aspose
License licPdf = new License();
FileInputStream fis = new FileInputStream("/java/bins/Aspose.Pdf.lic"); 
licPdf.setLicense(fis);	
fis.close();
			
//add path to look for fonts
List fontPaths = Document.getLocalFontPaths();
fontPaths.add("/java/fonts/");
Document.setLocalFontPaths(fontPaths);
		
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
//Load HTML file 
Document doc = new Document(args[1].trim(), htmloptions); - this is the line that it now crashes on
//Save HTML file 
doc.save(args[2].trim());                              

Please help.

Thanks,
Duane

@duanep026

We used following code snippet with Aspose.PDF for Java 20.3 and the output PDF was fine.

HtmlLoadOptions options = new HtmlLoadOptions();
options.getPageInfo().setWidth(PageSize.getPageLetter().getHeight());
options.getPageInfo().setHeight(PageSize.getPageLetter().getWidth());
options.getPageInfo().setMargin(new MarginInfo(0,0,0,0));
//options.getPageInfo().setHeight(1000); // in points 72pt = 1 inch
Document doc = new Document(dataDir + "180641.html", options);
doc.save(dataDir + "HTMLtoPDF_20.3.pdf");

HTMLtoPDF_20.3.pdf (56.6 KB)

Please try to copy all fonts from the system where code is running fine and paste them into the folder of the targeted system. We hope this will help in resolving the issue that you are facing. Furthermore, please try to use latest version of the API as it is always recommended to use latest version.

Thanks Asad.

I changed to now user aspose words and now it does run on the ibm iseries. But I am having an issue with my alignment again. Please assist me in keeping all the formatting the same.

Please find attached the html and the pdf that it is converted to.

Please find my code below:

//License for Aspose
License licDoc = new License();
FileInputStream fis = new FileInputStream("/java/bins/Aspose.Words.Java.lic"); 
licDoc.setLicense(fis);	
fis.close();
			
String htmlDoc = "/java/180641.html";
String pdfDoc  = "/java/180641.pdf";
if ( args.length > 1)
{
	htmlDoc = args[1].trim();
	pdfDoc = args[2].trim();
}
Document doc = new Document(htmlDoc);
ArrayList<FolderFontSource> fontSources = new ArrayList(Arrays.asList(FontSettings.getDefaultInstance().getFontsSources()));
FolderFontSource folderFontSource = new FolderFontSource("/java/fonts/", true);

fontSources.add(folderFontSource);
FontSourceBase[] updatedFontSources = (FontSourceBase[]) fontSources.toArray(new FontSourceBase[fontSources.size()]);
FontSettings.getDefaultInstance().setFontsSources(updatedFontSources); 			
			
for (Section section : doc.getSections())
{
	//section.getPageSetup().clearFormatting();
	//section.getPageSetup().setBorderDistanceFrom(1);
	section.getPageSetup().setPaperSize(PaperSize.LEGAL);
	section.getPageSetup().setOrientation(Orientation.LANDSCAPE);
	section.getPageSetup().setBorderAppliesTo(PageBorderAppliesTo.ALL_PAGES);
	section.getPageSetup().setBottomMargin(0.1);
	section.getPageSetup().setLeftMargin(0.1);
	section.getPageSetup().setTopMargin(0.1);
	section.getPageSetup().setRightMargin(0.1);
}

PdfSaveOptions saop = new PdfSaveOptions();
saop.setEmbedFullFonts(true);<a class="attachment" href="/uploads/default/36725">pdf.zip</a> (1.1 MB)
 
doc.save(pdfDoc, saop);//.set

Thanks for your assistance.

Regards,
Duane

Sorry I don’t think the attachments came through on the previous response. Here it is.
pdf.zip (1.1 MB)

@duanep026

We have moved your inquiry to Aspose.Words forum category where you will be assisted accordingly.

@duanep026,

The PDF file you shared (180641.pdf) was actually produced by using a very old version of Aspose.Words for Java i.e. 16.4. Can you please try the latest version of Aspose.Words for Java i.e. 20.3 and see how it goes on your end?

The code from the following article reveals that only ‘Verdana’ and ‘Times New Roman’ fonts are required for the correct rendering of this HTML to PDF format.

I have these fonts already installed in my System Fonts Folder so I do not need to execute code that looks for fonts in custom folders. Now, I have produced this PDF file on my end by using the following code:

Java Code:

Document doc = new Document("E:\\Temp\\pdf\\180641.html");

for (Section section : doc.getSections()) {
    section.getPageSetup().setPaperSize(PaperSize.LEGAL);
    section.getPageSetup().setOrientation(Orientation.LANDSCAPE);
    section.getPageSetup().setBorderAppliesTo(PageBorderAppliesTo.ALL_PAGES);
    section.getPageSetup().setBottomMargin(0.1);
    section.getPageSetup().setLeftMargin(0.1);
    section.getPageSetup().setTopMargin(0.1);
    section.getPageSetup().setRightMargin(0.1);
}

PdfSaveOptions saop = new PdfSaveOptions();
saop.setEmbedFullFonts(true);

doc.save("E:\\Temp\\pdf\\awjava-20.3.pdf", saop);

Do you see the same problem(s) in this 20.3 generated PDF? If yes, can you please also provide comparison screenshot(s) highlighting all the problematic areas in this PDF (with respect to the HTML) and attach them here for our reference? Thanks for your cooperation.

Hi there,
Please find attached the html and the pdf.
Firstly, there is a problem with the margins. The html goes across the whole page horizontally. That formatting needs to remain the same.
Secondly, the terms and conditions part runs over 1 line on the html. It runs over 2 lin
Please find attached the html and the generated pdf that you have produced.
Your assistance to get these 2 to look identical would be greatly appreciated.
Thanks,
Duane
pdf.zip (1.1 MB)
es on the pdf.

@duanep026,

In this case, you can improve PDF output by first adjusting PageSetup and then inserting HTML by using the following Java code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

for (Section section : doc.getSections()) {
    section.getPageSetup().setPaperSize(PaperSize.LEGAL);
    section.getPageSetup().setOrientation(Orientation.LANDSCAPE);
    section.getPageSetup().setBorderAppliesTo(PageBorderAppliesTo.ALL_PAGES);
    section.getPageSetup().setBottomMargin(0.1);
    section.getPageSetup().setLeftMargin(0.1);
    section.getPageSetup().setTopMargin(0.1);
    section.getPageSetup().setRightMargin(0.1);
}

String data = new String(Files.readAllBytes(Paths.get("E:\\Temp\\pdf (1)\\180641.html")));
builder.insertHtml(data);

PdfSaveOptions saop = new PdfSaveOptions();
saop.setEmbedFullFonts(true);

doc.save("E:\\Temp\\pdf (1)\\awjava-20.3-insertHtml.pdf", saop);

pdf.zip (1.1 MB)
Thank you very much. Looks much better but it still isn’t right.
First issue - On the pdf, the signature line is going onto the second line.
Second issue - the text is not very clear when opening the pdf. When the html is opened, all text is very clear.
Third issue - The margins need to come in a little because they are not an exact replica of the html.

Thank you very much for your help. It is much appreciated. I have attached the html and the newly generated pdf so you can see what I’m talking about.

Regards,
Duane

@duanep026,

I see that you are producing PDF file by using a very old Aspose.Words for Java 16.4 version and would like to suggest you to please upgrade to the latest version i.e. 20.3. I have also produced another PDF (see awjava-20.3-insertHtml.pdf (273.1 KB)) by using the following code (without specifying any margins):

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

for (Section section : doc.getSections()) {
    section.getPageSetup().setPaperSize(PaperSize.LEGAL);
    section.getPageSetup().setOrientation(Orientation.LANDSCAPE);
    section.getPageSetup().setBorderAppliesTo(PageBorderAppliesTo.ALL_PAGES);
}

String data = new String(Files.readAllBytes(Paths.get("E:\\Temp\\pdf (2)\\180641.html")));
builder.insertHtml(data);

PdfSaveOptions saop = new PdfSaveOptions();
saop.setEmbedFullFonts(true);

doc.save("E:\\Temp\\pdf (2)\\awjava-20.3-insertHtml.pdf", saop);

Regarding the second issue, I am afraid, I see no such problem when viewing the above PDF with Adobe Acrobat Reader DC at 100% zoom on my end. This may be a PDF reader specific problem.

Generally, Aspose.Words mimics the behavior of MS Word i.e. if you convert the HTML file to PDF by using Aspose.Words, the output will look similar to what MS Word might have produced.

Alternatively, you may also try using the Aspose.HTML for Java API to convert HTML files to PDF.

Please let me know if I can be of any further assistance.

pdf.zip (33.7 KB)

Thank you. I will speak to my boss about us getting the license for the latest jar.
I have attached the html and a snapshot of the pdf that you sent through.
Please have a look. I have highlighted in yellow on the screenshot the formatting that did come through from the html to the pdf.
Thanks again for your assistance.
Regards,
Duane

@duanep026,

Thanks for the additional information. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-20250. We will further look into the details of this problem and will keep you updated here on the status of correction. We apologize for your inconvenience.

Thank you. Much appreciated.

Hi there,
Just following up on the status of this. This has become quite urgent.
Thanks again for your assistance. It is much appreciated.
Regards,
Duane

@duanep026,

Regarding WORDSNET-20250, we have completed the analysis of this issue and the root cause has been identified.

We have noted your concern and will update you via this thread as soon as any estimates (ETA) or any more updates will be available. We apologize for any inconvenience.

Hi there,
I trust you are well.
Haven’t received any feedback on this.
Please assist.
Thank you.
Regards,
Duane

@duanep026,

Unfortunately, your issue is not resolved yet. We will inform you via this thread as soon as this issue will get resolved or any estimates may be available in future. We apologize for your inconvenience.