B4J Question Get PDF with GetString

MarcoRome

Expert
Licensed User
Longtime User
Hi All.
I have this need. Save a PDF file using the GetString method instead of GetInputStream
The file is saved, but obviously they are different and above all of different sizes.
For example GetInputStream produces a 98kb file
For example GetString produces a 176kb file

1642577800340.png


Strings are not saved in the right way in the case of GetString, even using GetString2 ("UTF8")


Of course GetInputStream work.
***** FIRST METHOD GetInputStream
B4X:
    '***** FIRST METHOD GetInputStream
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.axitech.it/sites/default/files/attachment/esempio_PDF.pdf")
    'j.GetRequest.SetContentType("application/pdf")
    'j.Download($"${addr}"$)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim risultato As InputStream = j.GetInputStream
        Dim out As OutputStream = File.OpenOutput(File.DirApp, "documentgetinputstring.pdf", False)
        File.Copy2(risultato, out)
        out.Close
        fx.ShowExternalDocument(File.GetUri(File.DirApp, "documentgetinputstring.pdf"))
    End If
    j.Release
'    '****** FINE PARTE PDF


'***** SECOND METHOD GetString
B4X:
'***** SECOND METHOD GetString
      
    'Da sito Web
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.axitech.it/sites/default/files/attachment/esempio_PDF.pdf")
    'j.GetRequest.SetContentType("application/pdf")
    'j.Download($"${addr}"$)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim res As String
        res = j.GetString2("UTF8")
        'res = j.GetString
        Log(res)
        Dim risultato As InputStream = StringToInputStream(res)
        Dim out As OutputStream = File.OpenOutput(File.DirApp, "documentgetstring.pdf", False)
        File.Copy2(risultato, out)
        out.Close
        fx.ShowExternalDocument(File.GetUri(File.DirApp, "documentgetstring.pdf"))
    End If
    j.Release
'    '****** FINE PARTE PDF

Any Suggestion ??
Thank you very much
Marco
 

Attachments

  • GetPdfExample.zip
    2.4 KB · Views: 137
  • documentgetstring.pdf
    175.8 KB · Views: 137
  • documentgetinputstring.pdf
    97 KB · Views: 142
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Already.
In any case I solved by encoding the pdf data in base64, modifying what is necessary, then decoding and rewriting the same in an InputStream. This is how it works.

B4X:
Sub AdXXX(texto As String) As InputStream
    Dim strResult As InputStream
    Dim cs As CompressedStreams
    Dim su As StringUtils
    Dim bt() As Byte
    Try
        'code need modified....
        '......
        'After
        bt = su.DecodeBase64(texto)
        strResult.InitializeFromBytesArray(bt, 0, bt.Length)
        Return strResult
    Catch
        Log(LastException)
        Return Null
    End Try
End Sub

Thank you anyway dear Don.
 
Upvote 0
Top