SathvikGanta commited on
Commit
c2df858
·
verified ·
1 Parent(s): aea88e6

Update pdf_to_svg.py

Browse files
Files changed (1) hide show
  1. pdf_to_svg.py +19 -16
pdf_to_svg.py CHANGED
@@ -9,23 +9,26 @@ def convert_pdf_to_svg(input_pdf, output_svg, width, height):
9
  width (float): Target width in inches.
10
  height (float): Target height in inches.
11
  """
12
- doc = fitz.open(input_pdf)
13
- svg_content = []
 
14
 
15
- # DPI for the conversion (72 DPI = 1 inch)
16
- dpi = 72
17
- scaling_x = width * dpi / doc[0].rect.width
18
- scaling_y = height * dpi / doc[0].rect.height
19
 
20
- for page_num in range(len(doc)):
21
- page = doc[page_num]
22
- matrix = fitz.Matrix(scaling_x, scaling_y)
23
- svg = page.get_svg_image(matrix=matrix) # Extract SVG with scaling
24
- svg_content.append(svg)
25
 
26
- # Wrap SVG content with specified dimensions
27
- svg_header = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}in" height="{height}in" viewBox="0 0 {width * dpi} {height * dpi}">'
28
- svg_footer = "</svg>"
29
 
30
- with open(output_svg, "w") as svg_file:
31
- svg_file.write(svg_header + "\n".join(svg_content) + svg_footer)
 
 
 
9
  width (float): Target width in inches.
10
  height (float): Target height in inches.
11
  """
12
+ try:
13
+ doc = fitz.open(input_pdf)
14
+ svg_content = []
15
 
16
+ # DPI for the conversion (72 DPI = 1 inch)
17
+ dpi = 72
18
+ scaling_x = width * dpi / doc[0].rect.width
19
+ scaling_y = height * dpi / doc[0].rect.height
20
 
21
+ for page_num in range(len(doc)):
22
+ page = doc[page_num]
23
+ matrix = fitz.Matrix(scaling_x, scaling_y)
24
+ svg = page.get_svg_image(matrix=matrix) # Extract SVG with scaling
25
+ svg_content.append(svg)
26
 
27
+ # Wrap SVG content with specified dimensions
28
+ svg_header = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}in" height="{height}in" viewBox="0 0 {width * dpi} {height * dpi}">'
29
+ svg_footer = "</svg>"
30
 
31
+ with open(output_svg, "w") as svg_file:
32
+ svg_file.write(svg_header + "\n".join(svg_content) + svg_footer)
33
+ except Exception as e:
34
+ raise ValueError(f"Error converting PDF to SVG: {str(e)}")