Re: Problem creating a custom Drawing Flowable to use with RML
Ray Côté <[email protected]> Thu, 29 Jun 2023 11:05:51 -0400
| Newsgroups | gmane.comp.python.reportlab.user |
|---|---|
| Message-ID | <CAG5tnzofv-xAdjDs75zNM2aAbV=hHQq02YYEiQgBptKdyeOsfw@mail.gmail.com> |
--===============1830034584560285409== Content-Type: multipart/alternative; boundary="000000000000f6cc2505ff460a3b" --000000000000f6cc2505ff460a3b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I've created an example RML plugInFlowable for Matplotlib charts. Available at: https://github.com/rgacote/reportlab-flowable-for-rml/tree/main On Tue, Jun 20, 2023 at 12:01=E2=80=AFPM Ray C=C3=B4t=C3=A9 <ray+rl@appropr= iatesolutions.com> wrote: > 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@approp= riatesolutions.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 tha= t >> 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 >> >> ### >> > --000000000000f6cc2505ff460a3b 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">I've created an example RML plugInFlowable for Matplotlib cha= rts.</div><div class=3D"gmail_default" style=3D"font-family:arial,sans-seri= f">Available at:=C2=A0<a href=3D"https://github.com/rgacote/reportlab-flowa= ble-for-rml/tree/main">https://github.com/rgacote/reportlab-flowable-for-rm= l/tree/main</a></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" = class=3D"gmail_attr">On Tue, Jun 20, 2023 at 12:01=E2=80=AFPM Ray C=C3=B4t= =C3=A9 <<a href=3D"mailto:ray%[email protected]">ray+rl@appr= opriatesolutions.com</a>> wrote:<br></div><blockquote class=3D"gmail_quo= te" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204= );padding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"= font-family:arial,sans-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" style=3D"font-family:arial,sans-serif">=C2=A0 =C2=A0 def= draw(self) -> None:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 """Dr= aw the chart."""<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 renderPDF.dr= aw(self.drawing, self.canv, 0, 0)<br></div><div class=3D"gmail_default" sty= le=3D"font-family:arial,sans-serif"><br></div><div class=3D"gmail_default" = style=3D"font-family:arial,sans-serif">I'll write the full implementati= on 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 <<a href=3D"mailto:ray%[email protected]" target=3D"_= blank">[email protected]</a>> wrote:<br></div><blockquote = class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px sol= id rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_= default" style=3D"font-family:arial,sans-serif">I'm trying to create an= SVG Flowable to use as a plugInFlowable with RML. I've looked at sever= al 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" st= yle=3D"font-family:arial,sans-serif"><br>My RML template contains the follo= wing:<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 <plugInFlowable module=3D"path= .to.charts" function=3D"chart">xyx</plugInFlowable>= <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's a self.canv obj= ect that has=C2=A0various draw commands (drawImage, drawString, etc.) but n= one of them draw a=C2=A0Drawing object.=C2=A0</div><div class=3D"gmail_defa= ult" style=3D"font-family:arial,sans-serif"><br></div><div class=3D"gmail_d= efault" style=3D"font-family:arial,sans-serif">I appreciate any guidance as= to what the draw function should be doing.<br>--Ray<br><br>class SvgFlowab= le(Flowable):<br>=C2=A0 =C2=A0 """Convert stream containing = SVG instructions to a Reportlab flowable."""<br><br>=C2=A0 = =C2=A0 def __init__(self, svg: BytesIO) -> None:<br>=C2=A0 =C2=A0 =C2=A0= =C2=A0 """Convert SVG to RML drawing on initialization.&quo= t;""<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 Drawi= ng<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.drawing._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) -> "D= rawing":<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 """Draw the cha= rt."""<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0self.drawing.draw().=C2= =A0 # Error<br><br>###<br></div></div> </blockquote></div> </blockquote></div> --000000000000f6cc2505ff460a3b-- --===============1830034584560285409== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline