UniUser Posted April 4, 2018 Share Posted April 4, 2018 Hello, we have an issue with pdf on IOS with Safari. If the pdf file is present on the server we can show it without problem using this code in a TUniLabel : href="path+fileName.pdf" target=\"_blank\" It is possible to show the pdf in a new page directly from a TmemoryStream without saving the file (all the files are stored as blob in a sql server db) We use sendstream and work on all browsers but due to IOS download restrictions in this case the procedure does not work. Any suggestion, alternative, workaround? Thank you in advance! Link to comment Share on other sites More sharing options...
UniUser Posted April 5, 2018 Author Share Posted April 5, 2018 Solved...i have used UniSession.AddJS with this code sample: window.open("data:application/pdf;base64, " +base64EncodedPDF); Where base64EncodedPDF is the pdf stream converted with Indy using TIdEncoderMIME. On IOS works good. Regards Link to comment Share on other sites More sharing options...
andyhill Posted April 6, 2018 Share Posted April 6, 2018 Great, can you show me your code to encode please ? Link to comment Share on other sites More sharing options...
andyhill Posted April 7, 2018 Share Posted April 7, 2018 I tried this code below but it locks up browser (chrome) in Desktop emulation (/m), file exists, endcoding works - can you please advise - thanks. base64EncodedPDF: String; ... procedure TMainmForm.EnCode64FileToStream(const FileName: string); var FileStream: TFileStream; s: String; begin try base64EncodedPDF:= ''; s:= ''; FileStream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); FileStream.Position:= 0; s:= TIdEncoderMIME.EncodeStream(FileStream); base64EncodedPDF:= 'window.open("data:application/pdf;base64, "'+s+');'; finally FileStream.Free; end; end; ... EnCode64FileToStream('test.pdf'); UniSession.AddJS(base64EncodedPDF); ... Link to comment Share on other sites More sharing options...
UniUser Posted April 9, 2018 Author Share Posted April 9, 2018 Hi i use this code, in ios with safari works good, the only issue is to remember to disable "block pop-ups" in the safari settings. uses IDCoder, IDCoderMime; var MStream:TBytesStream; NomeFile:string; PdfAnsi:AnsiString; Encoder: TIdEncoderMIME; Str:String; begin QTemp.Close; QTemp.SQL.Clear; QTemp.SQL.Add('select f.file_Stream,f.name,v.DocNumber,v.DocDate from v_Blob_Fatture Where f.stream_id='''+idBlob+''''); QTemp.Open; MStream:=TBytesStream.Create; TBlobField(Qtemp.FieldByName('File_Stream')).SaveToStream(MStream); NomeFile:='Fattura '+QTemp.FieldByName('DocNumber').AsString+' del '+StringReplace(QTemp.FieldByName('DocDate').AsString,'/','_',[rfReplaceAll])+'.pdf'; if (upiPhone in UniSession.UniPlatform) or (upiPad in UniSession.UniPlatform) then Begin PdfAnsi:= TIdEncoderMIME.EncodeBytes(TIdBytes(MStream.Bytes)); Str:='var base64EncodedPdf="'+PdfAnsi+'";'; Str:=Str+'window.open("data:application/pdf;base64," +base64EncodedPdf);'; UniSession.AddJS(Str); End else Begin UniSession.SendStream(MStream,NomeFile); End; MStream.Free; QTemp.Close; End; end; Link to comment Share on other sites More sharing options...
andyhill Posted April 13, 2018 Share Posted April 13, 2018 Thanks, I followed your logic with my code below, Desktop Downloads as expected, iPhone X does nothing ? FStream:= TFileStream.Create(UniServerModule.EXEDir+'web\'+UniMainModule.ReportFileName, fmOpenRead, fmShareDenyWrite); FStream.Position:= 0; BStream:= TBytesStream.Create; BStream.CopyFrom(FStream, FStream.Size); BStream.Position:= 0; FileName:= 'Report.pdf'; if (upiPhone in UniSession.UniPlatform) or (upiPad in UniSession.UniPlatform) then Begin PdfAnsi:= TIdEncoderMIME.EncodeBytes(TIdBytes(BStream.Bytes)); // IDCoder, IDCoderMime, IdGlobal Str:= 'var base64EncodedPdf="'+PdfAnsi+'"; '; Str:= Str+'window.open("data:application/pdf;base64," +base64EncodedPdf); '; UniSession.AddJS(Str); end else Begin UniSession.SendStream(BStream, FileName); end; BStream.Free; FStream.Free; Can you advise please ? Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now