Saving document takes a long time

Hi.
I’ve got a problem with save some HTML document to PDF format.
Operation doc.save(newFile); takes a very long time.

Here is code doing that:

System.out.println(“initializing”);
com.aspose.words.LoadOptions lo = new com.aspose.words.LoadOptions();
lo.setLoadFormat(LoadFormat.HTML);
lo.setResourceLoadingCallback(new HandleResourceLoading());
System.out.println(“creating document”);
com.aspose.words.Document doc = new Document(filePath, lo);
System.out.println(“saving document”);
DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
doc.save(newFile);
System.out.println(“document saved”);
Calendar cal2 = Calendar.getInstance();
System.out.println(dateFormat.format(cal2.getTime()));

Output is:

saving document
2014/05/05 14:48:36
document saved
2014/05/05 15:44:44
d:\asposetest/145.pdf

As You can see, method save() was running about 1 hour.
Source HTML file in attachment.

What is wrong?

Hi there,

Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v14.4.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v14.4.0 and let us know how it goes on your side. I hope, this will help.

If you still
face problem, please share the code of HandleResourceLoading. I will
investigate the issue on my side and provide you more information.

Moreover, it
is quite difficult to answer such questions because CPU performance and
memory usage all depend on complexity and size of the documents you are
loading/generating. Usually, Aspose.Words needs 10 times more memory
than the original document size to build it’s DOM in the memory and when
it comes to rendering a document to fixed page formats (e.g. PDF),
Aspose.Words needs to build two model in the memory – one for document
and the other for rendered document.

Hi.
Thanks for answer.
Upgrade of Asposer.Words resolved my problem.

Anyway, I have one more question.
When Aspose doesn’t find an image linked in HTML file, it puts an default image to PDF with red cross. But when lost image is bigger then default, it is scalled to size of lost image.
It looks ugly. Is there any possibility to change it?

Example in attachment.

Hi there,

Thanks for your inquiry. Please implement IResourceLoadingCallback interface to achieve your requirements. Implement this interface if you want to control how Aspose.Words loads external resource when importing a document from HTML or MHTML.

Hope this helps you. Please let us know if you have any more queries.

LoadOptions options = new LoadOptions(LoadFormat.HTML, "", "");

options.setResourceLoadingCallback(new IResourceLoadingCallback() {

public int resourceLoading(ResourceLoadingArgs args) {

if(args.getResourceType() == ResourceType.IMAGE)

{

try {

URL url = new URL(args.getOriginalUri());

Image image = ImageIO.read(url);

} catch (Exception e) {

try {

BufferedImage originalImage = ImageIO.read(new File("c:\\temp\\defaultimage.jpg"));

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageIO.write(originalImage, "jpg", baos );

baos.flush();

byte[] imageInByte = baos.toByteArray();

baos.close();

args.setData(imageInByte);

} catch (Exception ex) {

}

return ResourceLoadingAction.USER_PROVIDED;

}

return ResourceLoadingAction.DEFAULT;

}//end

else {

return ResourceLoadingAction.DEFAULT;

}

}

});

Document doc = new Document(MyDir + "in.htm", options);

doc.save(MyDir + "Out.pdf");