Question

first of, I don't know much about XSL.

I am using a app called DITA to generate pdfs. One of the things it requires is an overwrite of an xsl file; to add custom styling.

I am trying to add an external graphic using a relative path. It doesn't work, unless I supply the full path.

Does not work:

<fo:block text-align="center" width="100%">
  <fo:external-graphic src="../../images/logo.png"/>
</fo:block>

Does work:

<fo:block text-align="center" width="100%">
  <fo:external-graphic src="/absolute/path/to/images/logo.png"/>
</fo:block>

I looked on the web, it said to use "file:image.png" and other website said to use "url(image.png)", but neither worked.

What am I doing wrong?

Was it helpful?

Solution

This is an old question but something that is often misunderstood when working with DITA-OT.
The important thing to know is that the external-graphic path is relative to the DITA-OT artwork.dir.

I can think of two ways to do add the logo.

The easy way

Copy logo.png into the arwork diretory

DITA-OT/demo/fo/cfg/common/artwork/logo.png

Change your xsl graphic path to

<fo:block text-align="center" width="100%">
  <fo:external-graphic src="Configuration/OpenTopic/cfg/common/artwork/logo.png"/>
</fo:block>

The harder way

It is possible to change the artwork directory from which DITA takes it's pictures as well as the output directory which will be used by FOP to render the PDF.

open the build.xml file

DITA-OT/demo/fo/build.xml

Artwork root

Define the root directory where DITA should copy the artwork to The default is

<property name="artwork.dir" value="${dita.map.output.dir}"/>

Which will set the root to where your final PDF will be saved.

Artwork destination

Define the path where the files should be stored relative to the artwork root directory

<copy todir="${coreArtworkDestinationDir}/Configuration/OpenTopic"

By default it will create the folders /Configuration/OpenTopic and then copy everything in there including sub directories.
Make sure to change the destination twice. The two places you have to edit are only a few lines apart.

Artwork source

Define where the original artwork is saved so that DITA-OT can copy the files to the destination.
The first line points to the default artwork that come with DITA-OT and should not be changed.

<fileset dir="${basedir}" includes="cfg/common/artwork/**/*.*"/>

The second one is intended for customizing thus being the one that should be used for customization.

<fileset dir="${customization.dir}" includes="common/artwork/**/*.*"/>

The path is relative to DITA-OT/demo/fo/Customization.

OTHER TIPS

I had a similar problem and discovered in another forum that problem is the form to assign the "baseDir" path, because the baseDir path must have the prefix "file:".

Here a method in C# to create a PDF with images:

   private string CreatePDF(string fileToCreate, string templateFile)
   {
       org.apache.fop.configuration.Configuration.put("baseDir", "file:" + AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["ImagesPath"] + @"\");

       //Load the style sheet.
       XslCompiledTransform xslt = new XslCompiledTransform();
       xslt.Load(templateFile);

       //Execute the transform and output the results to a file.
       xslt.Transform(fileToCreate, "temp.fo");

       FileInputStream streamFO = new FileInputStream("temp.fo");
       InputSource src = new InputSource(streamFO);

       string pdfFilesPath = ConfigurationManager.AppSettings["PDFFilesPath"];
       if (!Directory.Exists(pdfFilesPath))
       {
           Directory.CreateDirectory(pdfFilesPath);
       }

       pdfFilesPath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["PDFFilesPath"];

       string fileName = fileToCreate.Substring(fileToCreate.LastIndexOf(@"\") + 1, fileToCreate.LastIndexOf(".") - 1 - fileToCreate.LastIndexOf(@"\")) + ".PDF";
       FileOutputStream streamOut = new FileOutputStream(pdfFilesPath + @"\" + fileName);
       Driver driver = new Driver(src, streamOut);

       driver.setRenderer(1);
       driver.run();
       streamOut.close();

       return fileName;
   }

Regards! Gabriel.

I'd say you've got a conflict between where you think you are and where the processing engine thinks it is. Absolute paths always work. Try to verify 'current' location and you'll see what's going on.

The path is relative to the location of the XML document, not the location of the stylesheet.

You need to set baseDir before referencing any external resource. This post can help you on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top