import base64
import os
import sys
import PyPDF2

svg = '''<svg id="write-document" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect id="write-doc-background" width="100%" height="100%" fill="#808080"/>
<defs id="write-defs">
<script type="text/writeconfig">
  <int name="docFormatVersion" value="2" />
  <int name="pageColor" value="-1" />
  <int name="pageNum" value="0" />
  <int name="ruleColor" value="0" />
  <float name="marginLeft" value="0" />
  <float name="xOffset" value="-380.701752" />
  <float name="xRuling" value="0" />
  <float name="yOffset" value="1536.84216" />
  <float name="yRuling" value="0" />
</script>
</defs>
'''

pdf_path = sys.argv[1]
pdf = PyPDF2.PdfFileReader(pdf_path, "rb")
img_width = 720
n_pages = pdf.getNumPages() + 1

page = pdf.getPage(0)
width = page.mediaBox.getWidth()
height = page.mediaBox.getHeight()
aspect_ratio = height/width
img_height = int(aspect_ratio * img_width)

os.system('mkdir -p /tmp/pdf2write')

new_page_height = 0

for page in range(n_pages):

    print(f"Processing {page}/{n_pages}", end='\r')

    os.system(f'pdftoppm {pdf_path} /tmp/pdf2write/tmp{page} -png -f {page} -singlefile')
    with open(f'/tmp/pdf2write/tmp{page}.png', 'rb') as f:
        base64_data = base64.b64encode(f.read()).decode('utf-8')

    tmp_svg = f'''<svg class="write-page" color-interpolation="linearRGB" x="10" y="{new_page_height+10}" width="{img_width}px" height="{img_height}px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <g class="write-content write-v3" width="{img_width}" height="{img_height}" xruling="0" yruling="0" marginLeft="0" papercolor="#FFFFFF" rulecolor="#00000000">
    <g class="ruleline write-std-ruling write-scale-down" fill="none" stroke="none" stroke-width="1" shape-rendering="crispEdges" vector-effect="non-scaling-stroke">
      <rect class="pagerect" fill="#FFFFFF" stroke="none" x="0" y="0" width="{img_width}" height="{img_height}" />
    </g>
    <image x="0" y="0" width="{img_width}" height="{img_height}" xlink:href="data:image/png;base64,{base64_data}"/>
  </g>
</svg>'''
    new_page_height += (img_height+10)
    svg += tmp_svg

svg += '''</svg>'''

os.system('rm -rf /tmp/pdf2write')

with open(f'{os.path.dirname(pdf_path)}/{os.path.basename(pdf_path).split(".")[0]}.svg', 'w') as f:
    f.write(svg)

os.system(f'gzip -S z {os.path.dirname(pdf_path)}/{os.path.basename(pdf_path).split(".")[0]}.svg')

[((24, 6, 24, 42), 'PyPDF2.PdfFileReader', 'PyPDF2.PdfFileReader', ({(24, 27, 24, 35): 'pdf_path', (24, 37, 24, 41): '"""rb"""'}, {}), "(pdf_path, 'rb')", False, 'import PyPDF2\n'), ((34, 0, 34, 36), 'os.system', 'os.system', ({(34, 10, 34, 35): '"""mkdir -p /tmp/pdf2write"""'}, {}), "('mkdir -p /tmp/pdf2write')", False, 'import os\n'), ((59, 0, 59, 34), 'os.system', 'os.system', ({(59, 10, 59, 33): '"""rm -rf /tmp/pdf2write"""'}, {}), "('rm -rf /tmp/pdf2write')", False, 'import os\n'), ((42, 4, 42, 89), 'os.system', 'os.system', ({(42, 14, 42, 88): 'f"""pdftoppm {pdf_path} /tmp/pdf2write/tmp{page} -png -f {page} -singlefile"""'}, {}), "(\n    f'pdftoppm {pdf_path} /tmp/pdf2write/tmp{page} -png -f {page} -singlefile')", False, 'import os\n'), ((64, 23, 64, 48), 'os.path.dirname', 'os.path.dirname', ({(64, 39, 64, 47): 'pdf_path'}, {}), '(pdf_path)', False, 'import os\n'), ((61, 13, 61, 38), 'os.path.dirname', 'os.path.dirname', ({(61, 29, 61, 37): 'pdf_path'}, {}), '(pdf_path)', False, 'import os\n'), ((64, 51, 64, 77), 'os.path.basename', 'os.path.basename', ({(64, 68, 64, 76): 'pdf_path'}, {}), '(pdf_path)', False, 'import os\n'), ((61, 41, 61, 67), 'os.path.basename', 'os.path.basename', ({(61, 58, 61, 66): 'pdf_path'}, {}), '(pdf_path)', False, 'import os\n')]