We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

HTML text background color not applying

Dear Team,

In the trail of Aspose PDF, i converted PDF to HTML. Conversion was done using JAVA.
While am trying to set background color to certain text in HTML file using style, the style is not getting applied. But all other styles are working except background color property to that text in HTML.

How to do that ?

@yatheendra

Thanks for contacting support.

Could you please share your sample PDF document along with complete code snippet you are using for conversion and changing the text background color. We will test the scenario in our environment and address it accordingly.

Dear Asad,

PDF_2.zip (2.0 MB)
Here i attached the HTML file i got after conversion from PDF.

Here is the code of conversion:

FileInputStream fstream = new FileInputStream(“C:\Users\admin\Documents\Aspose.Pdf.lic”);

        License license = new License();

license.setLicense(fstream);

	Document doc = new Document("C:\\Users\\admin\\Desktop\\drillbit_Plagiarism\\verylarge\\1.pdf");
	// tune conversion parameters
	HtmlSaveOptions newOptions = new HtmlSaveOptions();
	newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
	newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;
	newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;
	newOptions.LettersPositioningMethod = LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;
	newOptions.setSplitIntoPages(false);// force write HTMLs of all pages into one output document
	newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy() {
		public void invoke(HtmlSaveOptions.HtmlPageMarkupSavingInfo htmlSavingInfo) {
			try {
                                // TODO Auto-generated method stub
                                byte[] resultHtmlAsBytes = new byte[(int) htmlSavingInfo.ContentStream.available()];
                                htmlSavingInfo.ContentStream.read(resultHtmlAsBytes, 0, resultHtmlAsBytes.length);
                                // here You can use any writable stream, file stream is taken just as example
                                FileOutputStream fos;
                                try {
                                    fos = new FileOutputStream("C:\\Users\\admin\\Desktop\\drillbit_Plagiarism\\verylarge\\1.html");
                                    fos.write(resultHtmlAsBytes);
                                    fos.close();
                                } catch (FileNotFoundException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            } catch (IOException ex) {
				Logger.getLogger(Aspose.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	};
	// we can use some non-existing file name all real saving will be done in CustomerHtmlSavingStrategy
	String outHtmlFile = "C:\\Users\\admin\\Desktop\\drillbit_Plagiarism\\jntu1.html";
	doc.save(outHtmlFile, newOptions);

Here is the code of background color setting

String mat=“ANALYTICAL METHOD”;
StringBuilder contentBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(“C:\Users\admin\Desktop\PDF_2.html”));
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
}
in.close();
} catch (IOException e) {
}

      String content = contentBuilder.toString();
      content=content.replaceAll(mat,"<sup style='background-color:red'>1</sup><font style='background-color: rgba(51,51,51,1.0) !important; all:unset;'>"+mat+"</font>");
  //  System.out.println(content);
            
    BufferedWriter bw = new BufferedWriter(new FileWriter("yati.html"));
    bw.write(content);
    bw.close();

@yatheendra

We tried the code snippet which you shared for setting background color of specific text and were unable to notice the issue. We have used similar file that you shared and background color and other style was applied. For your kind reference, we have also attached the output generated at our side.
yati.zip (2.0 MB)

Could you please share a sample console application with us which is able to reproduce the issue. We will again test the scenario in our environment and share our feedback with you.

Dear @asad.ali,

Is this the right PDF to HTML conversion code we should use for “Developer Small Business license” ?


FileInputStream fstream = new FileInputStream(“C:\Users\admin\Documents\Aspose.Pdf.lic”);
License license = new License();
license.setLicense(fstream);

                    Document doc = new Document("input.pdf");
                    // tune conversion parameters
                    HtmlSaveOptions newOptions = new HtmlSaveOptions();
                    newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
                    newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;
                    newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;
                    newOptions.LettersPositioningMethod = LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;
                    newOptions.setSplitIntoPages(false);// force write HTMLs of all pages into one output document
                    newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy() {
                                    public void invoke(HtmlSaveOptions.HtmlPageMarkupSavingInfo htmlSavingInfo) {
                                                    try {
                                // TODO Auto-generated method stub
                                byte[] resultHtmlAsBytes = new byte[(int) htmlSavingInfo.ContentStream.available()];
                                htmlSavingInfo.ContentStream.read(resultHtmlAsBytes, 0, resultHtmlAsBytes.length);
                                // here You can use any writable stream, file stream is taken just as example
                                FileOutputStream fos;
                                try {
                                    fos = new FileOutputStream("output.html");
                                    fos.write(resultHtmlAsBytes);
                                    fos.close();
                                } catch (FileNotFoundException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            } catch (IOException ex) {
                                                                    Logger.getLogger(Aspose.class.getName()).log(Level.SEVERE, null, ex);
                                                    }
                                    }
                    };
                    // we can use some non-existing file name all real saving will be done in CustomerHtmlSavingStrategy
                    String outHtmlFile = " output.html";
                    doc.save(outHtmlFile, newOptions);

Please do confirm on the same

@yatheendra

The code you shared seems fine and generates HTML correctly. However, please note that Aspose.PDF for Java offers different ways to convert your PDF document into HTML. You may use any of the methods from the given link according to your requirements. In case you have some specific requirement which is not being achieved, please feel free to let us know. We will further proceed to assist you accordingly.

Dear @asad.ali

Thanks for the previous responses.
Now that i can convert PDF to HTML, i have a small issue, the converted html is not sitting with 100% width when I open it in the browser.
Here i attached both PDF and HTML converted file.
Help me out how can i set width of html to be fitted 100% when i open it in browser.
toaspose.zip (259.7 KB)

@yatheendra

We regret that feature to specify content width during PDF to HTML conversion is not currently present in the API. However, we have logged a feature request as PDFJAVA-38480 in our issue tracking system for the sake of its implementation. As soon as the feature is available, we will surely let you know. Please be patient and spare us little time.

We are sorry for the inconvenience.

@asad.ali
We thank you for the response and gesture for the request we made.

Coming to the conversion from HTML to PDF, we noticed that 1 page of HTML converted to PDF will be produced in 2 pages i.e. some part of content will be drawn to 2nd page (in the converted PDF).

We would be glad to know if there is an option to set the conversion to be in same number of pages like in HTML.

(1 or 2 ending part lines will be continued to the 2nd page.)
To make this clear we attach both HTML and PDF file here. Please consider this request and help us resolve it.
toaspose.zip (142.8 KB)

CODE used for Conversion

public static void convertHTMLFileToPDF() {
// Specify the The base path/url for the html file which serves as images database
String basePath = “pdftest”;
HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
// Load HTML file
Document doc = new Document(“C:\Users\admin\Desktop\Results\Result_1.html”, htmloptions);
// Save HTML file
doc.save(“C:\Users\admin\Desktop\Results\Result_1.pdf”);
}

public static void settingToNotPullDownRemoteResourcesDuringConversion() {
	HtmlLoadOptions options = new HtmlLoadOptions();
	options.CustomLoaderOfExternalResources = new LoadOptions.ResourceLoadingStrategy() {
		public LoadOptions.ResourceLoadingResult invoke(String resourceURI) {
			// Creating clear template resource for replacing:
			LoadOptions.ResourceLoadingResult res =
                                    new LoadOptions.ResourceLoadingResult(new byte[] {});
			// Return empty byte array in case i.imgur.com server
			if (resourceURI.contains("i.imgur.com")) {
				return res;
			} else {
				// Process resources with default resource loader
				res.LoadingCancelled = true;
				return res;
			}
		}
	};
	// Do conversion
	Document pdfDocument = new Document("C:\\Users\\admin\\Desktop\\Results\\Result_1.html",
                    options);
	pdfDocument.save("C:\\Users\\admin\\Desktop\\Results\\Result_1.pdf");
}

@yatheendra

You can set page height and width as well as margins during conversion of HTML into PDF. For example in following code snippet, we have removed the margins so that content could fit in one page. You can also specify page height and width in order to get custom PDF Page size.

HtmlLoadOptions options = new HtmlLoadOptions();
options.getPageInfo().setMargin(new MarginInfo(0,0,0,0));
//options.getPageInfo().setHeight(1000); // in points 72pt = 1 inch
Document doc = new Document(dataDir + "input.html", options);
doc.save(dataDir + "HTMLtoPDF_19.3.pdf");

HTMLtoPDF_19.3_Heightchanged.pdf (100.7 KB)
HTMLtoPDF_19.3_marginsremoved.pdf (100.7 KB)

@asad.ali

Thanks a ton for the support.
Yes i was working around with these options and could set them successfully.

I was checking for the option to set border for the PDF while converting from HTML to PDF but I couldn’t find the right article for it.

Can you please tell how can I set a simple border to the PDF ?

Awaiting your reply.

Thanks in Advance.

@yatheendra

There is no way to add page border during conversion of HTML to PDF. However, once PDF is generated, you can add border to it by using following code snippet:

Document doc = new Document(dataDir + "HTMLtoPDF_19.3_out.pdf");
for(Page page:doc.getPages())
{
 com.aspose.pdf.drawing.Graph graph = new com.aspose.pdf.drawing.Graph((float)page.getPageInfo().getWidth(), (float)page.getPageInfo().getHeight());
 page.getPageInfo().setMargin(new MarginInfo(0,0,0,0));
 page.getParagraphs().add(graph);
 com.aspose.pdf.drawing.Line bottomline = new com.aspose.pdf.drawing.Line(new float[] {(float)page.getMediaBox().getLLX(), (float)page.getMediaBox().getLLY(), (float)page.getMediaBox().getWidth(), (float)page.getMediaBox().getLLY()});
 com.aspose.pdf.drawing.Line topline = new com.aspose.pdf.drawing.Line(new float[] { 0, (float)page.getPageInfo().getHeight(), (float)page.getMediaBox().getWidth(), (float)page.getPageInfo().getHeight() });
 com.aspose.pdf.drawing.Line rightline = new com.aspose.pdf.drawing.Line(new float[] { (float)page.getMediaBox().getWidth(), 0, (float)page.getMediaBox().getWidth(), (float)page.getPageInfo().getHeight() });
 com.aspose.pdf.drawing.Line leftline = new com.aspose.pdf.drawing.Line(new float[] { 0, 0, 0, (float)page.getPageInfo().getHeight() });

 bottomline.getGraphInfo().setLineWidth(10);
 topline.getGraphInfo().setLineWidth(10);
 rightline.getGraphInfo().setLineWidth(10);
 leftline.getGraphInfo().setLineWidth(10);

 graph.getShapes().add(topline);
 graph.getShapes().add(bottomline);
 graph.getShapes().add(rightline);
 graph.getShapes().add(leftline);

 graph.setZIndex(0);
}
doc.save(dataDir + "borderusinggraph.pdf");