How to generate a PDF from a string

Asked

Viewed 65 times

0

After making an HTTP request to an API that promises to return one: "nrwexPDF"

I have the following return:

"%PDF-1.2 \r\n1 0 obj\r\n<<\r\n/Type /Catalog\r\n/Pages 3 0 R\r\n>>\r\nendobj\r\n2 0 obj\r\n<<\r\n/Producer (PowerPdf version 0.9)\r\n/CreationDate (D:20201117114152)\r\n/ModDate (D:20201117114152)\r\n>>\r\nendobj\r\n3 0 obj\r\n<<\r\n/Type /Pages\r\n/Kids [4 0 R ]\r\n/Count 1\r\n>>\r\nendobj\r\n4 0 obj\r\n<<\r\n/Type /Page\r\n/Parent 3 0 R\r\n/MediaBox [0 0 612 792 ]\r\n/Resources <<\r\n/Font <<\r\n/F0 6 0 R\r\n/F1 7 0 R\r\n>>\r\n/XObject <<\r\n>>\r\n/ProcSet [/PDF /Text /ImageC ]\r\n>>\r\n/Contents 5 0 R\r\n>>\r\nendobj\r\n5 0 obj\r\n<<\r\n/Length 623\r\n/Filter [/FlateDecode ]\r\n>>\r\nstream\r\nxÚÍ•ÍnÚ@\u0010€ï<Å\u001czH¤f½³¶¹mÀ¤Ž°ì%Ê!R…\u001a\u001a¥j‚Dh+õ\u0005+å-ú=õ\u0005º¶iwõ½ !¬ýf¾™ýs4£€\u0014ÌÇ\u0011%\u0012¾¢\u0019BÒ=BûÝ>Œ.͈Q’\nˆeBR0÷£3ª(\u0002èEy\u000bWuµ\\€©³¼ÔEVšª\fš¬¾É'öïÜL5\\@‘µ?«Ýöñû9˜O£Ì´YE\u001a\u0013Á½´\u0018Gˆ\u0011£Ì–„c°øù0¦”¢\u0017´/E(’².hR.®ÇÀ\u0018‰Q\u0011!dÔ\u0006\\Pé‹bÕ‰\\Ðb½ýµ¹ßŒbD÷Ê\u0015pŒv\u000f¾PôFkHe\u0017<]?mž_vÛÕîñë\u0006ŠõóËê3Üo`¶Ú}Ù®žÖÏ»\u001f]8Ú,q\u0017þþúx…¢\u001e… ’¾Ìâµñ»`Ò.Òß1]VþôRJâøßàL›e­§\u0015ÜÕoîÎ\u0003\u0006‰„ŠŽ½Öe–×Õ©È!GË!lŽVæú–Ò€„Å„&}AÙMV‡5\u000e\u001aÐ8`@C9Aì'L×?\u0002\n\u0007\f(\u001c\u0010VÈ8%Ø·ª/ë|~¢ð€°Â\u0003\u0006\u0014R\u0012Ü/»ÎO›ðÆ\u0007\f\u000e\u00180p$¬ïòzY¾\u000b(\u001c0 pÀ€‚Æ„í·Õr\u001eR8àXÁY§p@J\u0012\u0015Öˆ„\u0013¶?\u0006WUcN=\u001eqìaIëñD¢è€H¦„wP“™¬¸\fì_‡\fˆÜ¸m«°‡KÂ{ªZšeÐã\u0001‘\u00030%\"IÂ&Û-ïû6•Ñ\u0007;™óÖ Ã\r*NT\"\u000e\u001dÜ^ÿ\u0012¸=M\u001230Åì\u0018K‘ȾH]ä¥Ñ\r\u0014z’gs0Y~k/\bí§³{Á†Ø“#û\u001dÑüþðxp¯'Ø\u0002Êbª\u0011-fcHìËŒ\n[o¢.ØiyÌž\u0013yØà\u001f±ƒµ†\nendstream\r\nendobj\r\n6 0 obj\r\n<<\r\n/Type /Font\r\n/Subtype /Type1\r\n/Encoding /WinAnsiEncoding\r\n/FirstChar 32\r\n/LastChar 255\r\n/BaseFont /Helvetica\r\n/Name /F0\r\n>>\r\nendobj\r\n7 0 obj\r\n<<\r\n/Type /Font\r\n/Subtype /Type1\r\n/Encoding /WinAnsiEncoding\r\n/FirstChar 32\r\n/LastChar 255\r\n/BaseFont /Times-Roman\r\n/Name /F1\r\n>>\r\nendobj\r\nxref\r\n0 8\r\n0000000000 65535 f\r\n0000000011 00000 n\r\n0000000066 00000 n\r\n0000000188 00000 n\r\n0000000253 00000 n\r\n0000000453 00000 n\r\n0000001159 00000 n\r\n0000001305 00000 n\r\ntrailer\r\n<<\r\n/Size 8\r\n/Root 1 0 R\r\n/Info 2 0 R\r\n>>\r\nstartxref\r\n1453\r\n%%EOF\r\n";

I am trying to generate a pdf file from the string above:

I know that this string is with scape, so the original string to generate the pdf is:

%PDF-1.2 
1 0 obj
<<
/Type /Catalog
/Pages 3 0 R
>>
endobj
2 0 obj
<<
/Producer (PowerPdf version 0.9)
/CreationDate (D:20201117114152)
/ModDate (D:20201117114152)
>>
endobj
3 0 obj
<<
/Type /Pages
/Kids [4 0 R ]
/Count 1
>>
endobj
4 0 obj
<<
/Type /Page
/Parent 3 0 R
/MediaBox [0 0 612 792 ]
/Resources <<
/Font <<
/F0 6 0 R
/F1 7 0 R
>>
/XObject <<
>>
/ProcSet [/PDF /Text /ImageC ]
>>
/Contents 5 0 R
>>
endobj
5 0 obj
<<
/Length 623
/Filter [/FlateDecode ]
>>
stream
xÚÍ•ÍnÚ@€ï<ÅzH¤f½³¶¹mÀ¤Ž°ì%Ê!R…¥j‚Dh+õ+å-ú=õº¶iwõ½ !¬ýf¾™ýs4£€ÌÇ%¾¢BÒ=BûÝ>Œ.͈Q’
ˆeBR0÷£3ª(èEyWuµ\€©³¼ÔEVšªš¬¾É'öïÜL5\@‘µ?«Ýöñû9˜O£Ì´YEÁ½´Gˆ£Ì–„c°øù0¦”¢´/E(’².hR.®ÇÀ‰Q!dÔ\Pé‹bÕ‰\Ðb½ýµ¹ßŒbD÷ÊpŒv¾PôFkHe<]?mž_vÛÕîñëŠõóËê3Üo`¶Ú}Ù®žÖÏ»]8Ú,qþþúx…¢… ’¾Ìâµñ»`Ò.Òß1]VþôRJâøßàL›e­§ÜÕoîΉ„ŠŽ½Öe–×Õ©È!GË!lŽVæú–Ò€„Å„&}AÙMV‡5Ð8`@C9Aì'L×?
(VÈ8%Ø·ª/ë|~¢ð€°ÂRÜ/»ÎO›ðÆ0p$¬ïòzY¾(0 pÀ€‚Æ„í·ÕrR8àXÁY§p@JÖˆ„¶?WUcN=qìaIëñD¢è€H¦„wP“™¬¸ì_‡ˆÜ¸m«°‡KÂ{ªZšeÐã‘0%"IÂ&Û-ïû6•Ñ;™óÖ Ã
*NT"Ü^ÿ¸=M30ÅìK‘ȾH]ä¥Ñ
z’gs0Y~k/í§³{Á†Ø“#ûÑüþðxp¯'ØÊbª-fcHìËŒ
[o¢.ØiyÌžyØృµ†
endstream
endobj
6 0 obj
<<
/Type /Font
/Subtype /Type1
/Encoding /WinAnsiEncoding
/FirstChar 32
/LastChar 255
/BaseFont /Helvetica
/Name /F0
>>
endobj
7 0 obj
<<
/Type /Font
/Subtype /Type1
/Encoding /WinAnsiEncoding
/FirstChar 32
/LastChar 255
/BaseFont /Times-Roman
/Name /F1
>>
endobj
xref
0 8
0000000000 65535 f
0000000011 00000 n
0000000066 00000 n
0000000188 00000 n
0000000253 00000 n
0000000453 00000 n
0000001159 00000 n
0000001305 00000 n
trailer
<<
/Size 8
/Root 1 0 R
/Info 2 0 R
>>
startxref
1453
%%EOF

I also know that this code above is a structure of a pdf already ready.

Therefore, I would like to know if it is possible to generate a ". pdf" file again with this code.

In case more information is missing, I apologize, because it is my first interaction as questioner here in the stack.

INTEGRATED API RESPONSE:

{
    "PageCount": 1,
    "Size": 1599,
    "Data":"%PDF-1.2 \r\n1 0 obj\r\n<<\r\n/Type /Catalog\r\n/Pages 3 0 R\r\n>>\r\nendobj\r\n2 0 obj\r\n<<\r\n/Producer (PowerPdf version 0.9)\r\n/CreationDate (D:20201120122250)\r\n/ModDate (D:20201120122250)\r\n>>\r\nendobj\r\n3 0 obj\r\n<<\r\n/Type /Pages\r\n/Kids [4 0 R ]\r\n/Count 1\r\n>>\r\nendobj\r\n4 0 obj\r\n<<\r\n/Type /Page\r\n/Parent 3 0 R\r\n/MediaBox [0 0 612 792 ]\r\n/Resources <<\r\n/Font <<\r\n/F0 6 0 R\r\n/F1 7 0 R\r\n>>\r\n/XObject <<\r\n>>\r\n/ProcSet [/PDF /Text /ImageC ]\r\n>>\r\n/Contents 5 0 R\r\n>>\r\nendobj\r\n5 0 obj\r\n<<\r\n/Length 522\r\n/Filter [/FlateDecode ]\r\n>>\r\nstream\r\nxÚÍ”KnÛ0@÷:Å,ºH€†š!E}¼£c9•a[†D{ `7HÑX€ã¶@/X ·èz‡êã†LLy]iEÌ›y3$%Š@\bú“‡LÂwÏŸÄÝÚ÷ðàµÇ‘%D2f\tè­wE2‘wJ—P¦Å&»ÍKP“E¶ÌJ](mšõ\\OÜÀ¢:\\ƒþ쥺­$\u000b„UŒ£OäsäM#|Ä9¬~?Œ‘¬¤SAÈÞ%Ý.W³ˆa²ˆB¿M¸‰íœ \n;‘IZíêm=$OÊ\nù„ݽ‘sÖŒÚ&OvOõþùx¨ŽßjXìöÏÕØÖ0­Ž_ÕÓn¬í|êÒ©©uéÿ§Çj”ší\t\t¸osñ«´§à²9¤1µÌííEdQôœ*½.Ô$‡û«âÝýµÃ ‰aб3µL³\"?äÍqMVf|èðˆaÜ7”nÒ­1Ѐƌ¨ß0Uüt(\f0 0€[!£„Q?ªÙüLan…\f(¤dt:v•aÅ\f0\bb¼Ÿr¶^~p(\f0 0À€#ÆO×j=w)\fðV!x§0@š߈SÄ‚ñÓgp——úÜcn\\ðÈ„‰Ž)S.ÆŽëkÏKü‚FH&z(_ëµSc.ˆˆ˜è‡Ö¹V¯®±­Æ¶‚ÇÂÛ_ý+Ç_ů‰F\nendstream\r\nendobj\r\n6 0 obj\r\n<<\r\n/Type /Font\r\n/Subtype /Type1\r\n/Encoding /WinAnsiEncoding\r\n/FirstChar 32\r\n/LastChar 255\r\n/BaseFont /Helvetica\r\n/Name /F0\r\n>>\r\nendobj\r\n7 0 obj\r\n<<\r\n/Type /Font\r\n/Subtype /Type1\r\n/Encoding /WinAnsiEncoding\r\n/FirstChar 32\r\n/LastChar 255\r\n/BaseFont /Times-Roman\r\n/Name /F1\r\n>>\r\nendobj\r\nxref\r\n0 8\r\n0000000000 65535 f\r\n0000000011 00000 n\r\n0000000066 00000 n\r\n0000000188 00000 n\r\n0000000253 00000 n\r\n0000000453 00000 n\r\n0000001058 00000 n\r\n0000001204 00000 n\r\ntrailer\r\n<<\r\n/Size 8\r\n/Root 1 0 R\r\n/Info 2 0 R\r\n>>\r\nstartxref\r\n1352\r\n%%EOF\r\n",
    "Message": "",
    "ErrosRelatorio": []
}
  • You can use the file... but are sure that this is the return of the API and it is not you who is reading the answer as text?

  • Leandro, thanks for the feedback. I edited the question, with the answer of the API. I didn’t understand what you meant by using the file...

  • Is this your? api or your company’s? return content like this in a JSON utf-8 is a bad idea. If the api is returning a file, this should be a Base64 blob. If you convert the content presented in the question into a file. pdf the result will be a document in

  • If the api is not yours or your company’s, I recommend you contact them and request support for endpoint consumption

1 answer

0

A simple way to run the demand is to simply write the received content into a file .pdf (if your intention is to save that content)

//sendo contentString o retorno da sua api
var json = JsonConvert.DeserializeObject<dynamic>(contentString);
File.WriteAllText("c:\\temp\\data.pdf", json.Data.Value);
  • This way the generated file will be blank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.