Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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")
# 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)
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}")
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")
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'))
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]))
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)
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)
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)