Re: [docbook-apps] Embedding DocBook → PDF transfo rmation in a Java web app

Benjamin DE DARDEL <[email protected]> Fri, 28 Jul 2023 22:58:01 -1000
Newsgroups gmane.text.docbook.apps
Message-ID <CACeQubcW758ReptkUXNxmCHkEuE-C0okJ4vSSpSKdUrfg_4w6w@mail.gmail.com>
--000000000000c28dc206019c6559
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,

This is an old problem that I looked into some time ago.
I use my ant4docbook project daily to generate pdfs, from docbook,
markdown and docbook xsl1.xx.
Surely there are some interesting things to take in there.
The next step is to use xsltng + an html to pdf converter (chromium cef pdf=
?).

Regards,
Benjamin

Le ven. 28 juil. 2023 =C3=A0 22:35, Paul Hoadley <[email protected]> a =
=C3=A9crit :

> On 29 Jul 2023, at 13:48, Paul Hoadley <[email protected]> wrote:
>
> I'll put together a minimal example and see if you can put me in the righ=
t
> direction.
>
>
> To recap, I'm building a Java project that will encapsulate the DocBook
> stylesheets and some classes to do some transformations, all packaged as =
a
> JAR to become part of a larger web app. The project uses the standard Mav=
en
> layout, and Saxon-HE 12.3. I've put the XSL in src/main/resources/xsl:
>
> paulh@elmo xsl % pwd
> /Users/paulh/Projects/Java/janus/src/main/resources/xsl
> paulh@elmo xsl % ls -l
> total 688
> drwxr-xr-x@ 48 paulh  staff    1536 29 Jul  2020 docbook-xsl-1.79.2
> -rw-r--r--   1 paulh  staff   16681 24 Jul 10:41 header-footer.xsl
> -rw-r--r--   1 paulh  staff    7494 28 Jul 14:23 juno-driver.xsl
> -rw-r--r--   1 paulh  staff    8689 24 Jul 14:33 table.xsl
> -rw-r--r--   1 paulh  staff  307717 18 Jul 12:49 titlepage.xsl
>
> juno-driver.xsl is the top-level customisation stylesheet, and it imports
> the others:
>
>   <xsl:import href=3D"docbook-xsl-1.79.2/fo/docbook.xsl" />
>   <xsl:import href=3D"titlepage.xsl" />
>   <xsl:import href=3D"table.xsl" />
>   <xsl:import href=3D"header-footer.xsl" />
>
> To be clear, addressing the XSL stylesheets as files on the filesystem
> works just fine. That is, this does exactly what it should:
>
> private Document transformDocument(Document document) throws
> TransformerException, FileNotFoundException {
> DOMResult result =3D new DOMResult();
> TransformerFactory factory =3D TransformerFactory.newInstance();
> factory.setURIResolver(new StandardURIResolver());
> InputStream is =3D new FileInputStream(new
> File("/Users/paulh/Projects/Java/janus/src/main/resources/xsl/juno-driver=
.xsl"));
> Source source =3D new StreamSource(is,
> "file:/Users/paulh/Projects/Java/janus/src/main/resources/xsl/juno-driver=
.xsl");
> Transformer transformer =3D factory.newTransformer(source);
> transformer.transform(new DOMSource(document), result);
> return (Document) result.getNode();
> }
>
> What I want to do, though, is reference the stylesheets as classpath
> resources. StandardURIResolver claims to be able to handle the "classpath
> URI scheme", so I tried this:
>
> private Document transformDocument(Document document) throws
> TransformerException, FileNotFoundException {
> DOMResult result =3D new DOMResult();
> TransformerFactory factory =3D TransformerFactory.newInstance();
> factory.setURIResolver(new StandardURIResolver());
> InputStream is =3D XmlTest.class.getResourceAsStream("/xsl/juno-driver.xs=
l");
> Source source =3D new StreamSource(is, "classpath:/xsl/juno-driver.xsl");
> Transformer transformer =3D factory.newTransformer(source);
> transformer.transform(new DOMSource(document), result);
> return (Document) result.getNode();
> }
>
> Which results in:
>
> Error
>   XTSE0165: I/O error reported by XML parser processing
>   classpath:xsl/docbook-xsl-1.79.2/fo/docbook.xsl: unknown protocol:
> classpath
> javax.xml.transform.TransformerConfigurationException:
> net.sf.saxon.s9api.SaxonApiException: I/O error reported by XML parser
> processing classpath:xsl/docbook-xsl-1.79.2/fo/docbook.xsl
> at net.sf.saxon.jaxp.SaxonTransformerFactory.newTemplates(
> SaxonTransformerFactory.java:158)
> at net.sf.saxon.jaxp.SaxonTransformerFactory.newTransformer(
> SaxonTransformerFactory.java:112)
> at net.logicsquad.janus.XmlTest.transformDocument(XmlTest.java:142)
>
> If StandardURIResolver can handle classpath URIs, have I just got the
> syntax or other usage wrong somewhere?
>
>
> --
> Paul Hoadley
> https://logicsquad.net/
> https://www.linkedin.com/company/logic-squad/
>
>

--000000000000c28dc206019c6559
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div>Hi,<div dir=3D"auto"><br></div><div dir=3D"auto"><pr=
e dir=3D"ltr" style=3D"max-height:999999px;line-height:28px;border:none;pad=
ding:10px 0.14em 10px 0px;margin-top:0px;margin-bottom:0px;width:328px"><sp=
an style=3D"max-height:999999px">This is an old problem that I looked into =
some time ago.
I use my ant4docbook project daily to generate pdfs, from docbook, markdown=
 and docbook xsl1.xx.
Surely there are some interesting things to take in there.
The next step is to use xsltng + an html to pdf converter (chromium cef pdf=
?).</span></pre></div>Regards,=C2=A0</div><div dir=3D"auto">Benjamin=C2=A0<=
br><br><div class=3D"gmail_quote" dir=3D"auto"><div dir=3D"ltr" class=3D"gm=
ail_attr">Le ven. 28 juil. 2023 =C3=A0 22:35, Paul Hoadley &lt;<a href=3D"m=
ailto:[email protected]" target=3D"_blank" rel=3D"noreferrer">paulh@logi=
csquad.net</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex"><div style=3D"word-wrap:break-word;line-break:after-white-space">On 29 =
Jul 2023, at 13:48, Paul Hoadley &lt;<a href=3D"mailto:[email protected]=
" rel=3D"noreferrer noreferrer" target=3D"_blank">[email protected]</a>&=
gt; wrote:<div><br></div><div><div><blockquote type=3D"cite"><div><div styl=
e=3D"font-family:LucidaGrande;font-size:14px;font-style:normal;font-variant=
-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-in=
dent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decor=
ation:none"><div><div>I&#39;ll put together a minimal example and see if yo=
u can put me in the right direction.</div></div></div></div></blockquote><d=
iv><br></div><div>To recap, I&#39;m building a Java project that will encap=
sulate the DocBook stylesheets and some classes to do some transformations,=
 all packaged as a JAR to become part of a larger web app. The project uses=
 the standard Maven layout, and Saxon-HE 12.3. I&#39;ve put the XSL in src/=
main/resources/xsl:</div><div><br></div><div><div style=3D"margin:0px;font-=
stretch:normal;font-size:13px;line-height:normal;font-family:Menlo"><span s=
tyle=3D"font-variant-ligatures:no-common-ligatures">paulh@elmo xsl % pwd</s=
pan></div><div style=3D"margin:0px;font-stretch:normal;font-size:13px;line-=
height:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:no-c=
ommon-ligatures">/Users/paulh/Projects/Java/janus/src/main/resources/xsl</s=
pan></div><div style=3D"margin:0px;font-stretch:normal;font-size:13px;line-=
height:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:no-c=
ommon-ligatures">paulh@elmo xsl % ls -l</span></div><div style=3D"margin:0p=
x;font-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo">=
<span style=3D"font-variant-ligatures:no-common-ligatures">total 688</span>=
</div><div style=3D"margin:0px;font-stretch:normal;font-size:13px;line-heig=
ht:normal;font-family:Menlo"><span style=3D"font-variant-ligatures:no-commo=
n-ligatures">drwxr-xr-x@ 48 paulh=C2=A0 staff=C2=A0 =C2=A0 1536 29 Jul=C2=
=A0 2020 docbook-xsl-1.79.2</span></div><div style=3D"margin:0px;font-stret=
ch:normal;font-size:13px;line-height:normal;font-family:Menlo"><span style=
=3D"font-variant-ligatures:no-common-ligatures">-rw-r--r-- =C2=A0 1 paulh=
=C2=A0 staff =C2=A0 16681 24 Jul 10:41 header-footer.xsl</span></div><div s=
tyle=3D"margin:0px;font-stretch:normal;font-size:13px;line-height:normal;fo=
nt-family:Menlo"><span style=3D"font-variant-ligatures:no-common-ligatures"=
>-rw-r--r-- =C2=A0 1 paulh=C2=A0 staff=C2=A0 =C2=A0 7494 28 Jul 14:23 juno-=
driver.xsl</span></div><div style=3D"margin:0px;font-stretch:normal;font-si=
ze:13px;line-height:normal;font-family:Menlo"><span style=3D"font-variant-l=
igatures:no-common-ligatures">-rw-r--r-- =C2=A0 1 paulh=C2=A0 staff=C2=A0 =
=C2=A0 8689 24 Jul 14:33 table.xsl</span></div><div style=3D"margin:0px;fon=
t-stretch:normal;font-size:13px;line-height:normal;font-family:Menlo"><span=
 style=3D"font-variant-ligatures:no-common-ligatures">-rw-r--r-- =C2=A0 1 p=
aulh=C2=A0 staff=C2=A0 307717 18 Jul 12:49 titlepage.xsl</span></div><div><=
span style=3D"font-variant-ligatures:no-common-ligatures"><br></span></div>=
</div></div><div>juno-driver.xsl is the top-level customisation stylesheet,=
 and it imports the others:</div><div><br></div><div><div style=3D"margin:0=
px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;=
color:rgb(42,161,152)"><span style=3D"color:#000000">=C2=A0=C2=A0&lt;</span=
><span style=3D"color:#268bd2">xsl:import</span><span style=3D"color:#00000=
0"> </span><span style=3D"color:#93a1a1">href</span><span style=3D"color:#0=
00000">=3D</span><span style=3D"color:#93a1a1">&quot;</span>docbook-xsl-1.7=
9.2/fo/docbook.xsl<span style=3D"color:#93a1a1">&quot;</span><span style=3D=
"color:#000000"> /&gt;</span></div><div style=3D"margin:0px;font-stretch:no=
rmal;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(42,161,1=
52)"><span style=3D"color:#000000">=C2=A0 &lt;</span><span style=3D"color:#=
268bd2">xsl:import</span><span style=3D"color:#000000"> </span><span style=
=3D"color:#93a1a1">href</span><span style=3D"color:#000000">=3D</span><span=
 style=3D"color:#93a1a1">&quot;</span>titlepage.xsl<span style=3D"color:#93=
a1a1">&quot;</span><span style=3D"color:#000000"> /&gt;</span></div><div st=
yle=3D"margin:0px;font-stretch:normal;font-size:12px;line-height:normal;fon=
t-family:Menlo;color:rgb(38,139,210)"><span style=3D"color:#000000">=C2=A0 =
&lt;</span>xsl:import<span style=3D"color:#000000"> </span><span style=3D"c=
olor:#93a1a1">href</span><span style=3D"color:#000000">=3D</span><span styl=
e=3D"color:#93a1a1">&quot;</span><span style=3D"color:#2aa198">table.xsl</s=
pan><span style=3D"color:#93a1a1">&quot;</span><span style=3D"color:#000000=
"> /&gt;</span></div><div style=3D"margin:0px;font-stretch:normal;font-size=
:12px;line-height:normal;font-family:Menlo;color:rgb(42,161,152)"><span sty=
le=3D"color:#000000">=C2=A0 &lt;</span><span style=3D"color:#268bd2">xsl:im=
port</span><span style=3D"color:#000000"> </span><span style=3D"color:#93a1=
a1">href</span><span style=3D"color:#000000">=3D</span><span style=3D"color=
:#93a1a1">&quot;</span>header-footer.xsl<span style=3D"color:#93a1a1">&quot=
;</span><span style=3D"color:#000000"> /&gt;</span></div></div><div><br></d=
iv><div>To be clear, addressing the XSL stylesheets as files on the filesys=
tem works just fine. That is, this does exactly what it should:</div><div><=
br></div><div><div><font face=3D"Menlo" size=3D"2"><span style=3D"font-styl=
e:normal">private Document transformDocument(Document document) throws Tran=
sformerException, FileNotFoundException {</span></font></div><div><font fac=
e=3D"Menlo" size=3D"2"><span style=3D"font-style:normal"><span style=3D"whi=
te-space:pre-wrap">	</span>DOMResult result =3D new DOMResult();</span></fo=
nt></div><div><font face=3D"Menlo" size=3D"2"><span style=3D"font-style:nor=
mal"><span style=3D"white-space:pre-wrap">	</span>TransformerFactory factor=
y =3D TransformerFactory.newInstance();</span></font></div><div><font face=
=3D"Menlo" size=3D"2"><span style=3D"font-style:normal"><span style=3D"whit=
e-space:pre-wrap">	</span>factory.setURIResolver(new StandardURIResolver())=
;</span></font></div><div><font face=3D"Menlo" size=3D"2"><span style=3D"fo=
nt-style:normal"><span style=3D"white-space:pre-wrap">	</span>InputStream i=
s =3D new FileInputStream(new File(&quot;/Users/paulh/Projects/Java/janus/s=
rc/main/resources/xsl/juno-driver.xsl&quot;));</span></font></div><div><fon=
t face=3D"Menlo" size=3D"2"><span style=3D"font-style:normal"><span style=
=3D"white-space:pre-wrap">	</span>Source source =3D new StreamSource(is, &q=
uot;file:/Users/paulh/Projects/Java/janus/src/main/resources/xsl/juno-drive=
r.xsl&quot;);</span></font></div><div><font face=3D"Menlo" size=3D"2"><span=
 style=3D"font-style:normal"><span style=3D"white-space:pre-wrap">	</span>T=
ransformer transformer =3D factory.newTransformer(source);</span></font></d=
iv><div><font face=3D"Menlo" size=3D"2"><span style=3D"font-style:normal"><=
span style=3D"white-space:pre-wrap">	</span>transformer.transform(new DOMSo=
urce(document), result);</span></font></div><div><font face=3D"Menlo" size=
=3D"2"><span style=3D"font-style:normal"><span style=3D"white-space:pre-wra=
p">	</span>return (Document) result.getNode();</span></font></div><div><fon=
t face=3D"Menlo" size=3D"2"><span style=3D"font-style:normal">}</span></fon=
t></div></div><div><br></div><div>What I want to do, though, is reference t=
he stylesheets as classpath resources.=C2=A0StandardURIResolver claims to b=
e able to handle the &quot;classpath URI scheme&quot;, so I tried this:</di=
v><div><br></div><div><div><span style=3D"font-style:normal"><font face=3D"=
Menlo" size=3D"2">private Document transformDocument(Document document) thr=
ows TransformerException, FileNotFoundException {</font></span></div><div><=
span style=3D"font-style:normal"><font face=3D"Menlo" size=3D"2"><span styl=
e=3D"white-space:pre-wrap">	</span>DOMResult result =3D new DOMResult();</f=
ont></span></div><div><span style=3D"font-style:normal"><font face=3D"Menlo=
" size=3D"2"><span style=3D"white-space:pre-wrap">	</span>TransformerFactor=
y factory =3D TransformerFactory.newInstance();</font></span></div><div><sp=
an style=3D"font-style:normal"><font face=3D"Menlo" size=3D"2"><span style=
=3D"white-space:pre-wrap">	</span>factory.setURIResolver(new StandardURIRes=
olver());</font></span></div><div><span style=3D"font-style:normal"><font f=
ace=3D"Menlo" size=3D"2"><span style=3D"white-space:pre-wrap">	</span>Input=
Stream is =3D XmlTest.class.getResourceAsStream(&quot;/xsl/juno-driver.xsl&=
quot;);</font></span></div><div><span style=3D"font-style:normal"><font fac=
e=3D"Menlo" size=3D"2"><span style=3D"white-space:pre-wrap">	</span>Source =
source =3D new StreamSource(is, &quot;classpath:/xsl/juno-driver.xsl&quot;)=
;</font></span></div><div><span style=3D"font-style:normal"><font face=3D"M=
enlo" size=3D"2"><span style=3D"white-space:pre-wrap">	</span>Transformer t=
ransformer =3D factory.newTransformer(source);</font></span></div><div><spa=
n style=3D"font-style:normal"><font face=3D"Menlo" size=3D"2"><span style=
=3D"white-space:pre-wrap">	</span>transformer.transform(new DOMSource(docum=
ent), result);</font></span></div><div><span style=3D"font-style:normal"><f=
ont face=3D"Menlo" size=3D"2"><span style=3D"white-space:pre-wrap">	</span>=
return (Document) result.getNode();</font></span></div><div><span style=3D"=
font-style:normal"><font face=3D"Menlo" size=3D"2">}</font></span></div></d=
iv><div><br></div><div>Which results in:</div><div><br></div><div><div styl=
e=3D"margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-=
family:Menlo;color:rgb(255,0,0)">Error=C2=A0</div><div style=3D"margin:0px;=
font-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;col=
or:rgb(255,0,0)">=C2=A0 XTSE0165: I/O error reported by XML parser processi=
ng</div><div style=3D"margin:0px;font-stretch:normal;font-size:12px;line-he=
ight:normal;font-family:Menlo;color:rgb(255,0,0)">=C2=A0 classpath:xsl/docb=
ook-xsl-1.79.2/fo/docbook.xsl: unknown protocol: classpath</div><div style=
=3D"margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-f=
amily:Menlo;color:rgb(255,0,0)"><span style=3D"text-decoration:underline;co=
lor:#0068da">javax.xml.transform.TransformerConfigurationException</span>: =
<span style=3D"text-decoration:underline;color:#0068da">net.sf.saxon.s9api.=
SaxonApiException</span>: I/O error reported by XML parser processing class=
path:xsl/docbook-xsl-1.79.2/fo/docbook.xsl</div><div style=3D"margin:0px;fo=
nt-stretch:normal;font-size:12px;line-height:normal;font-family:Menlo;color=
:rgb(255,0,0)"><span style=3D"white-space:pre-wrap">	</span>at net.sf.saxon=
.jaxp.SaxonTransformerFactory.newTemplates(<span style=3D"text-decoration:u=
nderline;color:#0068da">SaxonTransformerFactory.java:158</span>)</div><div =
style=3D"margin:0px;font-stretch:normal;font-size:12px;line-height:normal;f=
ont-family:Menlo;color:rgb(255,0,0)"><span style=3D"white-space:pre-wrap">	=
</span>at net.sf.saxon.jaxp.SaxonTransformerFactory.newTransformer(<span st=
yle=3D"text-decoration:underline;color:#0068da">SaxonTransformerFactory.jav=
a:112</span>)</div><div style=3D"margin:0px;font-stretch:normal;font-size:1=
2px;line-height:normal;font-family:Menlo;color:rgb(255,0,0)"><span style=3D=
"white-space:pre-wrap">	</span>at net.logicsquad.janus.XmlTest.transformDoc=
ument(<span style=3D"text-decoration:underline;color:#0068da">XmlTest.java:=
142</span>)</div></div><div><br></div><div>If StandardURIResolver can handl=
e classpath URIs, have I just got the syntax or other usage wrong somewhere=
?</div><div><br></div><br><div>
<div style=3D"color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-=
indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wra=
p:break-word;line-break:after-white-space"><div style=3D"color:rgb(0,0,0);l=
etter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;w=
hite-space:normal;word-spacing:0px;word-wrap:break-word;line-break:after-wh=
ite-space"><span style=3D"border-collapse:separate;font-variant-ligatures:n=
ormal;font-variant-numeric:normal;font-variant-alternates:normal;font-varia=
nt-east-asian:normal;font-weight:normal;line-height:normal;border-spacing:0=
px"><div style=3D"word-wrap:break-word;line-break:after-white-space">--=C2=
=A0<br>Paul Hoadley<br><a href=3D"https://logicsquad.net/" rel=3D"noreferre=
r noreferrer" target=3D"_blank">https://logicsquad.net/</a><br><a href=3D"h=
ttps://www.linkedin.com/company/logic-squad/" rel=3D"noreferrer noreferrer"=
 target=3D"_blank">https://www.linkedin.com/company/logic-squad/</a></div><=
div style=3D"word-wrap:break-word;line-break:after-white-space"><br></div><=
/span></div></div></div></div></div></blockquote></div>
</div></div>

--000000000000c28dc206019c6559--