0

I want to make a id card generator using python. The output image should be accurate in size like 90mm x 67 mm rectangles on a A4 sheet size papper. i tried to make the file using svg. This is my code.

from xml.etree import ElementTree as Et

doc = Et.Element('svg',
                 width="297.0mm",
                 height="210.0mm",
                 version='1.1',
                 xmlns="http://www.w3.org/2000/svg")
Et.SubElement(doc, 'rect', x='0', y='0', width='297.0mm', height='210.0mm', fill='grey')

Et.SubElement(doc, 'path', transform="translate(10,20)",
              d="M 0.04181236,14.041812 V 361.04181 a 13,13 0 0 0 12.99999964,13 H 240.04181 a 13,13 0 0 0 13,"
                "-13 V 14.041812 a 13,13 0 0 0 -13,-13.99999967 H 14.041812 A 13,13 0 0 0 0.04181236,14.041812 Z",
              fill='red')


# _________________________Front________________________________________________________________

Et.SubElement(doc, 'path', transform="translate(10,20)",
              d="m 0.04181236,227.04181 v 134 a 13,13 0 0 0 13.99999964,14 H 240.04181 a 13,13 0 0 0 13,"
                "-13 V 99.041812 C 181.04181,218.04181 19.041812,227.04181 0.04181236,227.04181 Z",
              fill='white')

Et.SubElement(doc, 'image',
              href="photo.jpg",
              preserveAspectRatio="none",
              height="24mm",
              width="19mm",
              x="25.5mm",
              y="20.5mm",
              )

Et.SubElement(doc, 'path',
              d="m 104.48819,75.590553 h 55.59055 a 10,10 45 0 1 10,10 v 74.488187 a 10,10 135 0 1 -10,10 h -55.59055 "
                "a 10,10 45 0 1 -10,-10 V 85.590553 a 10,10 135 0 1 10,-10 z",
              style="opacity:0.989042;fill:none;stroke:#ffffff;stroke-width:5")

# _______________________________________________Back_____________________________________________________

Et.SubElement(doc, 'path', transform="translate(10,400)",
              d="M 0.04181236,14.041812 V 361.04181 a 13,13 0 0 0 12.99999964,13 H 240.04181 a 13,13 0 0 0 13,"
                "-13 V 14.041812 a 13,13 0 0 0 -13,-13.99999967 H 14.041812 A 13,13 0 0 0 0.04181236,14.041812 Z",
              fill='red')

Et.SubElement(doc, 'path', transform="translate(10,400)",
              d="m 0.04181236,227.04181 v 134 a 13,13 0 0 0 13.99999964,14 H 240.04181 a 13,13 0 0 0 13,"
                "-13 V 99.041812 C 181.04181,218.04181 19.041812,227.04181 0.04181236,227.04181 Z",
              fill='white')

with open('sample.svg', 'w') as f:
    f.write('\n')
    f.write('\n')
    f.write(Et.tostring(doc).decode())

This was my first try to do this. but the image is not appearing when i open it using inkscape and the other drawings can see there. Also , when i open the sample.svg in browser. i can see the image on it.

this is the output

Is this a correct way to solve this problem ? or i should choose any other methods.

SOORYADEV
  • 39
  • 1
  • 7
  • Too busy to test but try replacing `href="photo.jpg"` with a [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). See [this SO post](https://stackoverflow.com/a/8499716/5386938) for reference. –  Jun 08 '21 at 17:51
  • Thank you @JustinEzequiel for your suggestion , i converted the file to base64 then add to the file. the problem is solved. thanks again : ) – SOORYADEV Jun 09 '21 at 08:37

0 Answers0