I want to save a string from a blob I received through a Javascript injection/API POST through Selenium execute_script() in Python on a homepage to receive a PDF, as the actual PDF on my computer.
Together with the POST (includes charset=UTF-8) and some other things I did this in the execute_script():
const data = await downloadResponse.blob()
const reader = new FileReader()
reader.readAsBinaryString(data)
const str = await new Promise((resolve) => {reader.onload = () => {resolve(reader.result)}})
return str
and saved the result in “test”.
test then looks something like the very long string (I shortened it, it is a whole PDF file as a string after all if I understand correctly) at the end of this description.
How would I go about saving that as an actual PDF now?
I did try
config = pdfkit.configuration(wkhtmltopdf=r"C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe")
pdfkit.from_string(test,'test.pdf', configuration=config)
and
with open('test.pdf', 'wb') as output_file:
writer = PdfWriter()
stream = io.BytesIO()
stream.write(pdfkit.from_string(test, False, configuration=config))
writer.add_page(PdfReader(stream).pages[0])
writer.write(output_file)
to no avail. They both save a file called test.pdf but it is not actually a readable PDF. Its just a bunch of text starting like the long raw string.
I have the idea that I need to format or cut the string before passing it through those functions, but I have no idea how.
‘%PDF-1.4n%ª«¬xadn1 0 objn<<n/Creator (Apache FOP Version 2.7)n/Producer (Apache FOP Version 2.7)n/CreationDate (D:20230802212617+02’00’)n>>nendobjn2 0 objn<<n /N 3n /Length 3 0 Rn /Filter /FlateDecoden>>nstreamnxx9cíx99gPTYx16x80ï{x9dx13rÝMx93¡ÉI¢x84x06$ç$9x8ant7x99x16x9ax0c*x8ax0cx8eÀx08″”Ix11Dx14pÀÑ!È(*¢x18x10x05x05TÔidx10PÆÁQDEeiü1»5?¶¶jkÿlx9fx1fï}uî©wÎ}u«ÞWõx00x90!&°
[…]
øD¤Âà«G1Âcx13hlx0cx01vx81x01x91¨ïx18I6AÌäx1fx04PAgèÿx01ex95ö¥nendstreamnendobjn24 0 objn35932nendobjn23 0 objn<< /Length 25 0 R /Filter /FlateDecode >>nstreamnxx9cx9b”{ÿrÿói.ü»¿¿aà“`rx00x00Tmx07Dnendstreamnendobjn25 0 objn27nendobjn26 0 objn<<n /Type /Fontn /Subtype /Type0n /BaseFont /EAAAAB+TimesNewRomanPSMTn /Encoding /Identity-Hn /ToUnicode 27 0 Rn /DescendantFonts [28 0 R]n>>nendobjn28 0 objn<< /Type /Fontn/BaseFont /EAAAAB+TimesNewRomanPSMT n/CIDToGIDMap /Identity n/Subtype /CIDFontType2n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >>n/FontDescriptor 21 0 Rn/DW 0n/W [ 0 [777 250 333 722 443 333 277 500 389 500 277 722 777 443 500 556 277 500 500 500 610 443 333 333 408 500 666 500 722 250 500 556 500 500 500 666 722 443 250 722 500 666 500 500 500 500 500 889 500 500 389 722 500 610 556 610 443 277 500 500 ] ]n>>nendobjn27 0 objn<< /Length 29 0 R /Filter /FlateDecode >>nstreamnxx9c]x94Éx8aÛ@x10x86ï~x8a>Nx0ex83ÔÕZf`0x04x87x80x0fYx88x93x07h©[x8e
[…]
x9ax93÷g&x95å5x05AÛúm¯x91Çn··y^×:½EÒ>ox9bÜx0fññ:x9aÆix8bJx9f¿x9eZ&^nendstreamnendobjn29 0 objn494nendobjn30 0 objn<<n /Type /Fontn /Subtype /Type1n /BaseFont /Times-Romann /Encoding /WinAnsiEncodingn>>nendobjn11 0 objn<< /Type /Pagesn/Count 1n/Kids [9 0 R ] >>nendobjn31 0 objn<<n /Type /Catalogn /Pages 11 0 Rn /Lang (x-unknown)n /Metadata 5 0 Rn /PageLabels 32 0 Rn>>nendobjn10 0 objn<<n /Font <<n /F180 17 0 Rn /F179 26 0 Rn /F5 30 0 Rn>>n /ProcSet [/PDF /ImageB /ImageC /Text]n /ColorSpace << /DefaultRGB 4 0 R >>n>>nendobjn32 0 objn<< /Nums [0 << /S /D >>] >>nendobjnxrefn0 33n0000000000 65535 f n0000000015 00000 n n0000000145 00000 n n0000002702 00000 n n0000002722 00000 n n0000002755 00000 n n0000003727 00000 n n0000003746 00000 n n0000005509 00000 n n0000005529 00000 n n0000067900 00000 n n0000067727 00000 n n0000005753 00000 n n0000006021 00000 n n0000028773 00000 n n0000028751 00000 n n0000028870 00000 n n0000028889 00000 n n0000029455 00000 n n0000029058 00000 n n0000029941 00000 n n0000029961 00000 n n0000030224 00000 n n0000066275 00000 n n0000066253 00000 n n0000066378 00000 n n0000066397 00000 n n0000067029 00000 n n0000066561 00000 n n0000067599 00000 n n0000067619 00000 n n0000067786 00000 n n0000068057 00000 n ntrailern<<n /Root 31 0 Rn /Info 1 0 Rn /ID [<29F32EB815B73A68DE5ED9078778CB2B> <29F32EB815B73A68DE5ED9078778CB2B>]n /Size 33n>>nstartxrefn68101n%%EOFn’