Free Support Forum - aspose.cloud

Creating a PDF from Text in Cloud Environment


#1

I’m trying to create a PDF file from a text file in a cloud environment where I don’t have any control over fonts and font paths.


I’m my code is based on the example provided in your documentation.

Document pdfDocument = new Document();
Page page = pdfDocument.getPages().add();
page.getPageInfo().setMargin(new MarginInfo(36, 36, 36, 36));
Paragraphs paragraphs = page.getParagraphs();
Font font = FontRepository.findFont(“Courier”);

try (BufferedReader reader = new BufferedReader(new InputStreamFileReader(new FileInputStream(“textfile.txt”), “UTF-8”))) {
String strLine;
// read the file line by line
while ((strLine = reader.readLine()) != null) {
TextFragment textFragment = new TextFragment(strLine);
textFragment.getTextState().setFont(font);
paragraphs.add(textFragment);
}
} catch (IOException e) {
throw new Exception(“Unable to create PDF file from text”, e);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
pdfDocument.save(outputStream);

When using Aspose.PDF for Java 9.7.0 the code runs successfully, but when I upgrade to Aspose.PDF for Java 10.8.0 then I get the following error:

2015-10-15 17:50:28.026 ERROR 31 — [io-61143-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is class com.aspose.pdf.internal.ms.System.z9: Some font was not found. Please check if your font path is present in Document.getLocalFontPath() if not - add it with Document.addLocalFontPath(string)
com.aspose.pdf.internal.p49.z3.m1(Unknown Source)
com.aspose.pdf.internal.p50.z1.m1(Unknown Source)
com.aspose.pdf.internal.p51.z9.m1(Unknown Source)
com.aspose.pdf.TextSegment.setText(Unknown Source)
com.aspose.pdf.TextSegment.m1(Unknown Source)
com.aspose.pdf.TextBuilder.m1(Unknown Source)
com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
com.aspose.pdf.z63.m1(Unknown Source)
com.aspose.pdf.z63.m6(Unknown Source)
com.aspose.pdf.Page.m2(Unknown Source)
com.aspose.pdf.Page.processParagraphs(Unknown Source)
com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
com.aspose.pdf.Document.processParagraphs(Unknown Source)
com.aspose.pdf.ADocument.saveInternal(Unknown Source)
com.aspose.pdf.Document.saveInternal(Unknown Source)
com.aspose.pdf.ADocument$1.m1(Unknown Source)
com.aspose.pdf.internal.p718.z2.m2(Unknown Source)
com.aspose.pdf.internal.p718.z3.m1(Unknown Source)
com.aspose.pdf.ADocument.save(Unknown Source)
com.aspose.pdf.Document.save(Unknown Source)
demo.PdfController.createPdf(PdfController.java:156)

I can’t even tell which font it is looking for. The FontRepository.findFont(“Courier”) returns a font object, so I’m assuming that the font is found. I’ve also tried setting the DefaultTextState on the Document’s PageInfo to use the Courier font, but that doesn’t work either.

Thanks, Ian.



#2

Hi Ian,


Thanks for your inquiry. We have logged an investigation ticket PDFNEWJAVA-35212 in our issue tracking system for further investigation and rectification. We will keep you updated about the issue resolution progress within this forum thread.

We are sorry for the inconvenience caused.

Best Regards,

#3

Is there any update on this issue? It is preventing us from moving to the newer versions of Aspose.PDF.

Thanks, Ian.


#4

Hi Ian,


Thanks for your patience.

We have further investigated the earlier reported issue and have tested it on AWS Cloud (Amazon Linux) and I cannot reproduce the issue. Please note that in this issue, the Font “Courier” is taken from the resources of aspose.pdf-..jar (Courier.afm). Maybe your environment presents some restrictions for using the internal resources. Can you please share some more details about your cloud environment and meanwhile you may consider testing the latest Aspose.Pdf for Java 11.1.0 release version as we have fixed some problems related to fonts on Linux platform.

Furthermore, can you please confirm if you are using valid license during your testing?

#5
Hi Nayyer,

I am testing using Pivotal's Cloud Foundry as my cloud environment. My test application is a simple Spring MVC web application. That performs the code I posted above.

I still get the same error as before, just a bit more descriptive using Aspose PDF 11.1.0.

2016-01-20 19:30:05.323 ERROR 31 --- [io-62569-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is class com.aspose.pdf.internal.ms.System.z9: The font Courierwas not found. Please check if your font path is present in Document.getLocalFontPath() if not - add it with Document.addLocalFontPath(string)
com.aspose.pdf.internal.p54.z3.m1(Unknown Source)
com.aspose.pdf.internal.p55.z1.m1(Unknown Source)
com.aspose.pdf.internal.p56.z9.m1(Unknown Source)
com.aspose.pdf.TextSegment.setText(Unknown Source)

I was able to manually load the font file from the jar file into a byte array stream using the following code:

String fontLocationStr = "/com/aspose/pdf/engine/commondata/text/fonts/StandartFonts/Courier.afm";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (InputStream is = PdfController.class.getResourceAsStream(fontLocationStr)) {
IOUtils.copy(is, baos);
} catch (Exception x) {
log.error("Error loading font", x);
}
log.info("Font Size: " + baos.size());

The size logged is 15677, which matches the file in the jar. PdfController is the class I'm running the code from.

My license is valid, as I use it with other Aspose products.

Thanks, Ian.


#6

Hi Ian,


Thanks for sharing the details.

The information has been shared with product team and they are further looking into this matter. As soon as we have some further updates, we will let you know.

#7

Hi Nayyer,



I’m still able to reproduce this issue with the latest version of Aspose PDF 11.3.0.



Is there any update on this issue?



Thanks, Ian,


#8

Hi Ian,


Thanks for your patience.

The product team has further investigated the issue PDFNEWJAVA-35212 reported earlier and we are unable to reproduce the exception on our config for Pivotal’s Cloud Foundry. Please take a look on our result at: http://test3.cfapps.io/hello

We have also tested versions 11.3.0, 11.2.0 and 10.8.0 and all versions work fine. The following configuration was used for our test application:

Staging…
-----> Java Buildpack Version: v3.6 | https://github.com/cloudfoundry/java-buildpack.git#5194155
-----> Downloading Open Jdk JRE 1.8.0_73 from https://download.run.pivotal.io/openjdk/trusty/x86_64/openjdk-1.8.0_73.tar.gz(found in cache)
Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.1s)
-----> Downloading Open JDK Like Memory Calculator 2.0.1_RELEASE from https://download.run.pivotal.io/memory-calculator/trusty/x86_64/memory-calculator-2.0.1_RELEASE.tar.gz (found in cache)
Memory Settings: -XX:MetaspaceSize=104857K -Xss1M -Xmx768M -XX:MaxMetaspaceSize=104857K -Xms768M
-----> Downloading Tomcat Instance 8.0.32 from https://download.run.pivotal.io/tomcat/tomcat-8.0.32.tar.gz (found in cache)
Expanding Tomcat Instance to .java-buildpack/tomcat (0.1s)
-----> Downloading Tomcat Lifecycle Support 2.5.0_RELEASE from https://download.run.pivotal.io/tomcat-lifecycle-support/tomcat-lifecycle-support-2.5.0_RELEASE.jar (found in cache)
-----> Downloading Tomcat Logging Support 2.5.0_RELEASE from https://download.run.pivotal.io/tomcat-logging-support/tomcat-logging-support-2.5.0_RELEASE.jar (found in cache)
-----> Downloading Tomcat Access Logging Support 2.5.0_RELEASE from https://download.run.pivotal.io/tomcat-access-logging-support/tomcat-access-logging-support-2.5.0_RELEASE.jar (found in cache)

Can you please share some further information which can help to reproduce the problem.

#9

Hi Nayyer,



I can still reproduce the issue with version 11.3.0 and 11.2.0, my sample does work with 9.7.0.



My application is a Spring Boot application with a web interface.

I’ve attached my project to this message. The Readme.txt file has instructions on how to use it.



I’ve added to my test program the ability to create an MS Word file uses Aspose Words 16.2.0, and this works. Aspose Words is able to find the Courier font when creating its document. Does Aspose PDF use/load fonts differently that Aspose Words.



Here is the Cloud Foundry deployment log:

Starting app ian-demo in org EMC / space ianspace as ian@emc.com

-----> Downloaded app package (53M)

-----> Downloaded app buildpack cache (4.0K)

-----> Java Buildpack Version: v3.5.1 (offline) | https://github.com/cloudfoundry/java-buildpack.git#3abc3db

-----> Downloading Oracle JRE 1.8.0_72 from http://127.0.0.1/oracle-jre/trusty/x86_64/jre-8u72-linux-x64.tar.gz (found in cache)

Expanding Oracle JRE to .java-buildpack/oracle_jre (2.8s)

-----> Downloading Open JDK Like Memory Calculator 2.0.1_RELEASE from https://download.run.pivotal.io/memory-calculator/trusty/x86_64/memory-calculator-2.0.1_RELEASE.tar.gz (found in cache)

Memory Settings: -Xmx382293K -XX:MaxMetaspaceSize=64M -Xss995K -Xms382293K -XX:MetaspaceSize=64M

-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://download.run.pivotal.io/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (found in cache)



Let me know if there is anything else you need to help diagnose this.



Thanks, Ian.


#10

Hi Ian,


Thanks for sharing the details.

The information and resource project has been associated with earlier reported issue and product team is further looking into this matter. As soon as we have some further updates, we will let you know.

#11
Hi Ian,

Thanks for your patience. In reference to PDFJAVA-35212 issue, our product team has fixed some fonts related issues in latest release. We will appreciate it you please test Aspose.Pdf for Java 17.1.0 in your environment and share the results. Hopefully new version will help you to accomplish your requirements.

Looking forward to your feedback.

Best Regards,