Hi,
I am looking for the API that can convert a web page to a pdf file by giving a URL. I saw the API sample as below:
String basePath = “C:/pdftest/”;
com.aspose.pdf.HtmlLoadOptions htmloptions = new com.aspose.pdf.HtmlLoadOptions(basePath);
// use the new conversion engine
htmloptions.setUseNewConversionEngine(true);
// load HTML file
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(basePath+“EmailDemo_updated.html”, htmloptions);
// Save HTML file
doc.save(“c:/pdftest/Web+URL_output.pdf”);
Can I assign the basePath with a web URL, such as “http://www.myhost.com/mydir/
”?
And then I want put a web page URL to new a pdf.Document:
new com.aspose.pdf.Document(basePath + “MyPage.jsp”, htmloptions);
Does it work for any web URL?
Thanks!
Hi Kevin,
The doc listed above shows only C# and Dot.Net. Is it supported in Java?
Hi Kevin,
Hi Kevin,
We have a class named HtmlFragment
which provides the feature to add HTML contents inside PDF file. In order to convert URL contents to PDF format, first you need to get the contents from the internet, instantiate HtmlFragment
object using contents retrieved from Internet and then try placing the HtmlFragment inside the PDF document.
However during my testing, I have observed that our API is having some issues when using the following code snippet. For the sake of correction, I have logged this problem as PDFNEWJAVA-34479 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.
[Java]
try {
java.net.URL url = new java.net.URL("http://www.aspose.com");
InputStream is =
url.openConnection().getInputStream();
BufferedReader reader =
new BufferedReader(new java.io.InputStreamReader(is));
String line = null;
while((line = reader.readLine()) != null )
{
System.out.println(line);
}
reader.close();
// Instantiate HtmlFragment with HTML contents
com.aspose.pdf.HtmlFragment
contents = new
com.aspose.pdf.HtmlFragment(line);
System.out.println(contents);
com.aspose.pdf.Document
doc = new com.aspose.pdf.Document();
doc.getPages().add();
doc.getPages().get_Item(1).getParagraphs().add(contents);
doc.
save("c:/pdftest/URLContents.pdf");
System.out.println("Done");
}
catch(java.net.MalformedURLException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}