Re: Problem creating a custom Drawing Flowable to use with RML

Ray Côté <[email protected]> Tue, 20 Jun 2023 12:01:35 -0400
Newsgroups gmane.comp.python.reportlab.user
Message-ID <CAG5tnzovP7Z3hp6BSuurTJbdfKfpc+CtB1RCdJN58onWibTZ8A@mail.gmail.com>
--===============5461745747961102801==
Content-Type: multipart/alternative; boundary="000000000000c8912d05fe91c574"

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

Found the solution.

    def draw(self) -> None:
        """Draw the chart."""
        renderPDF.draw(self.drawing, self.canv, 0, 0)

I'll write the full implementation up as a Gist.

On Tue, Jun 20, 2023 at 9:22=E2=80=AFAM Ray C=C3=B4t=C3=A9 <ray+rl@appropri=
atesolutions.com>
wrote:

> I'm trying to create an SVG Flowable to use as a plugInFlowable with RML.
> I've looked at several online examples and they all show how to add the
> Diagram when generating the report directly with Python code.
>
> My RML template contains the following:
>         <plugInFlowable module=3D"path.to.charts"
> function=3D"chart">xyx</plugInFlowable>
>
> When I try to draw the Flowable using the following class I get an error
> that the Drawing does not contain a canv. There's a self.canv object that
> has various draw commands (drawImage, drawString, etc.) but none of them
> draw a Drawing object.
>
> I appreciate any guidance as to what the draw function should be doing.
> --Ray
>
> class SvgFlowable(Flowable):
>     """Convert stream containing SVG instructions to a Reportlab
> flowable."""
>
>     def __init__(self, svg: BytesIO) -> None:
>         """Convert SVG to RML drawing on initialization."""
>         svg.seek(0)
>         self.drawing: Drawing =3D svg2rlg(svg) # Convert SVG to Drawing
>         self.width: int =3D self.drawing.minWidth()
>         self.height: int =3D self.drawing.height
>         self.drawing._showBoundary =3D True
>
>     def wrap(self, *args):
>         return (self.width, self.height)
>
>     def draw(self) -> "Drawing":
>         """Draw the chart."""
>        self.drawing.draw().  # Error
>
> ###
>

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

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:arial,sa=
ns-serif">Found the solution.=C2=A0</div><div class=3D"gmail_default" style=
=3D"font-family:arial,sans-serif"><br></div><div class=3D"gmail_default" st=
yle=3D"font-family:arial,sans-serif">=C2=A0 =C2=A0 def draw(self) -&gt; Non=
e:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &quot;&quot;&quot;Draw the chart.&quot;&q=
uot;&quot;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 renderPDF.draw(self.drawing, self=
.canv, 0, 0)<br></div><div class=3D"gmail_default" style=3D"font-family:ari=
al,sans-serif"><br></div><div class=3D"gmail_default" style=3D"font-family:=
arial,sans-serif">I&#39;ll write the full implementation up as a Gist.</div=
></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr"=
>On Tue, Jun 20, 2023 at 9:22=E2=80=AFAM Ray C=C3=B4t=C3=A9 &lt;<a href=3D"=
mailto:ray%[email protected]">[email protected]</=
a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0p=
x 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><d=
iv dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:arial,sans=
-serif">I&#39;m trying to create an SVG Flowable to use as a plugInFlowable=
 with RML. I&#39;ve looked at several online examples and they all show how=
 to add the Diagram when generating the report directly with Python code.</=
div><div class=3D"gmail_default" style=3D"font-family:arial,sans-serif"><br=
>My RML template contains the following:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt=
;plugInFlowable module=3D&quot;path.to.charts&quot; function=3D&quot;chart&=
quot;&gt;xyx&lt;/plugInFlowable&gt;<br><br>When I try to draw the Flowable =
using the following class I get an error that the Drawing does not contain =
a canv. There&#39;s a self.canv object that has=C2=A0various draw commands =
(drawImage, drawString, etc.) but none of them draw a=C2=A0Drawing object.=
=C2=A0</div><div class=3D"gmail_default" style=3D"font-family:arial,sans-se=
rif"><br></div><div class=3D"gmail_default" style=3D"font-family:arial,sans=
-serif">I appreciate any guidance as to what the draw function should be do=
ing.<br>--Ray<br><br>class SvgFlowable(Flowable):<br>=C2=A0 =C2=A0 &quot;&q=
uot;&quot;Convert stream containing SVG instructions to a Reportlab flowabl=
e.&quot;&quot;&quot;<br><br>=C2=A0 =C2=A0 def __init__(self, svg: BytesIO) =
-&gt; None:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 &quot;&quot;&quot;Convert SVG to=
 RML drawing on initialization.&quot;&quot;&quot;<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 svg.seek(0)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.drawing: Drawing =3D=
 svg2rlg(svg) # Convert SVG to Drawing<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.=
width: int =3D self.drawing.minWidth()<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.=
height: int =3D self.drawing.height<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 self.dra=
wing._showBoundary =3D True<br><br>=C2=A0 =C2=A0 def wrap(self, *args):<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return (self.width, self.height)<br><br>=C2=A0 =
=C2=A0 def draw(self) -&gt; &quot;Drawing&quot;:<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 &quot;&quot;&quot;Draw the chart.&quot;&quot;&quot;<br>=C2=A0 =C2=A0=
 =C2=A0 =C2=A0self.drawing.draw().=C2=A0 # Error<br><br>###<br></div></div>
</blockquote></div>

--000000000000c8912d05fe91c574--

--===============5461745747961102801==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline