How to use the cairosvg.svg2pdf function in CairoSVG

To help you get started, we’ve selected a few CairoSVG examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fossasia / badgeyay / v1 / app / merge_badges.py View on Github external
def generate_pdfs(folder_path):
    """
    Function to generate the PDF for the badge
    :param `folder_path` - Path of the folder for saving of the PDF's
    """
    svgs = [file for file in os.listdir(folder_path) if file.lower().endswith('.svg')]
    for svg in svgs:
        svg_path = os.path.join(folder_path, svg)
        pdf_path = os.path.splitext(svg_path)[0] + '.pdf'
        print('svg: {}'.format(svg_path))
        print('pdf: {}'.format(pdf_path))
        try:
            svg2pdf(url=svg_path, write_to=pdf_path)
        except Exception as e:
            pass
github fossasia / badgeyay / api / utils / merge_badges.py View on Github external
def generate_pdfs(folder_path):
        svgs = [file for file in os.listdir(folder_path) if file.endswith('.svg')]
        for svg in svgs:
            svg_path = os.path.join(folder_path, svg)
            pdf_path = os.path.splitext(svg_path)[0] + '.pdf'
            try:
                svg2pdf(url=svg_path, write_to=pdf_path)
            except Exception:
                print('')
        print("done")
github mbr / svglue / example.py View on Github external
# replace the pink box with 'hello.png'. if you do not specify the mimetype,
# the image will get linked instead of embedded
tpl.set_image('pink-box', file='hello.png', mimetype='image/png')

# svgs are merged into the svg document (i.e. always embedded)
tpl.set_svg('yellow-box', file='Ghostscript_Tiger.svg')

# to render the template, cast it to a string. this also allows passing it
# as a parameter to set_svg() of another template
src = str(tpl)

# write out the result as an SVG image and render it to pdf using cairosvg
import cairosvg
with open('output.pdf', 'wb') as out, open('output.svg', 'w') as svgout:
    svgout.write(src)
    cairosvg.svg2pdf(bytestring=src, write_to=out)
github hoffa / notation / render.py View on Github external
def export_pdf(self, filename, width, height):
        self.svg.saveas("out.svg")
        cairosvg.svg2pdf(
            bytestring=self.svg.tostring(),
            write_to=filename,
            parent_width=width + (2 * self.margin),
            parent_height=height + (2 * self.margin),
        )
        print(f"Rendered to {filename}")
github bavovanachte / sphinx-wavedrom / sphinxcontrib / wavedrom.py View on Github external
fpath = os.path.join(outpath, fname)
        svgout.saveas(fpath)
        return fname

    # It gets a bit ugly, if the output does not support svg. We use cairosvg, because it is the easiest
    # to use (no dependency on installed programs). But it only works for Python 3.
    try:
        import cairosvg
    except:
        raise SphinxError(__("Cannot import 'cairosvg'. In Python 2 wavedrom figures other than svg are "
                             "not supported, in Python 3 ensure 'cairosvg' is installed."))

    if format == 'application/pdf':
        fname = "{}.{}".format(bname, "pdf")
        fpath = os.path.join(outpath, fname)
        cairosvg.svg2pdf(svgout.tostring(), write_to=fpath)
        return fname

    if format == 'image/png':
        fname = "{}.{}".format(bname, "png")
        fpath = os.path.join(outpath, fname)
        cairosvg.svg2png(svgout.tostring(), write_to=fpath)
        return fname

    raise SphinxError("No valid wavedrom conversion supplied")
github viscom-ulm / Net2Vis / backend / server.py View on Github external
def save_and_convert_svgs(base_path, json_content):
  """Save and convert the svg files into PDFs

  Arguments:
      base_path {String} -- The base path for this operation to be executed at
      json_content {object} -- The json content of the network
  """
  file = open(os.path.join(base_path, 'graph.svg'), 'w')
  file.write(json_content['graph'])
  file.flush()
  file.close()
  cairosvg.svg2pdf(url=os.path.join(base_path, 'graph.svg'),
                   write_to=os.path.join(base_path, 'graph.pdf'))
  file = open(os.path.join(base_path, 'legend.svg'), 'w')
  file.write(json_content['legend'])
  file.flush()
  file.close()
  cairosvg.svg2pdf(url=os.path.join(base_path, 'legend.svg'),
                   write_to=os.path.join(base_path, 'legend.pdf'))
github eventoL / eventoL / eventol / manager / views.py View on Github external
def view_ticket(request, event_slug):
    event_user = EventUser.objects.filter(
        event__event_slug=event_slug).filter(user=request.user).first()
    if event_user:
        ticket = generate_ticket(event_user)
        response = HttpResponse(svg2pdf(bytestring=ticket), content_type='application/pdf')
        response["Content-Disposition"] = 'filename=Ticket-' + str(event_user.ticket.code) + '.pdf'
        return response
    messages.error(request, "You are not registered for this event")
    return redirect(reverse("index", args=[event_slug]))
github MacroConnections / DIVE-backend / server / api / api.py View on Github external
filename = 'visualization.%s' % format
        fout = open(filename, 'wb')

        mimetypes = {
            'svg': 'image/svg',
            'pdf': 'application/pdf',
            'png': 'image/png'
        }

        img_io = StringIO()
        bytestring = bytes(svg)
        if format == "png":
            cairosvg.svg2png(bytestring=bytestring, write_to=fout)
            cairosvg.svg2png(bytestring=bytestring, write_to=img_io)
        elif format == "pdf":
            cairosvg.svg2pdf(bytestring=bytestring, write_to=fout)
            cairosvg.svg2pdf(bytestring=bytestring, write_to=img_io)
        elif format == "svg":
            cairosvg.svg2svg(bytestring=bytestring, write_to=fout)
            cairosvg.svg2svg(bytestring=bytestring, write_to=img_io)
        else:
            cairosvg.svg2png(bytestring=bytestring, write_to=fout)
            cairosvg.svg2png(bytestring=bytestring, write_to=img_io)
        fout.close()

        img_io.seek(0)
        return send_file(img_io)  #, mimetype=mimetypes[format], as_attachment=True, attachment_filename=filename)
github UbiquityRobotics / fiducials / aruco_detect / scripts / create_markers.py View on Github external
def genMarker(i, dicno, paper_size):
    print " Marker %d\r" % i,
    sys.stdout.flush()
    aruco_dict = aruco.Dictionary_get(dicno)
    img = aruco.drawMarker(aruco_dict, i, 2000)
    cv2.imwrite("/tmp/marker%d.png" % i, img)
    svg = genSvg(i, dicno, paper_size)
    cairosvg.svg2pdf(bytestring=svg, write_to='/tmp/marker%d.pdf' % i)
    os.remove("/tmp/marker%d.png" % i)
github algoo / preview-generator / preview_generator / preview / builder / image__cairosvg.py View on Github external
def build_pdf_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        extension: str = ".pdf",
        page_id: int = -1,
        mimetype: str = "",
    ) -> None:
        """
        generate pdf preview. No default implementation
        """
        preview_file_path = "{path}{extension}".format(
            path=cache_path + preview_name, extension=extension
        )
        cairosvg.svg2pdf(url=file_path, write_to=preview_file_path)