Forum Xamarin.Android

How to Save PDF in custom location from WebView's CreatePrintDocumentAdapter in Xamarin Android

HI Everyone,

I tried with this link, but i could not able to create instance for PrintDocumentAdapter.WriteResultCallback and it throws exception due to class is abstract restriction.

https://stackoverflow.com/questions/26841501/save-pdf-from-webview-on-android

Requirement is same as stated in above link.

Can you please suggest me to achieve the requirement?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    I convert to C# the PrintDocumentAdapter.WriteResultCallback.

          [Register("android/print/MYPrintDocumentAdapter")]
    public class MYPrintDocumentAdapter : LayoutResultCallback
    {
        private PrintDocumentAdapter printAdapter;
          public MYPrintDocumentAdapter(PrintDocumentAdapter printAdapter,IntPtr javaReference, JniHandleOwnership transfer) : base(IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
        {
            this.printAdapter = printAdapter;
        }
    
        public override void OnLayoutFinished(PrintDocumentInfo info, bool changed)
        {
    
            File sdf = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
            ParcelFileDescriptor fileDescriptor = getOutputFile(sdf, "SMV_Report");
            printAdapter.OnWrite(new PageRange[] { PageRange.AllPages }, fileDescriptor, new CancellationSignal(),
             new MyWriteResultCallback(JNIEnv.Handle, JniHandleOwnership.DoNotRegister));
    
            base.OnLayoutFinished(info, changed);
        }
        public ParcelFileDescriptor getOutputFile(File path, String fileName)
        {
            if (!path.Exists())
            {
                path.Mkdirs();
            }
            File file = new File(path, fileName);
            try
            {
                file.CreateNewFile();
                return ParcelFileDescriptor.Open(file, ParcelFileMode.ReadWrite);
            }
            catch (Exception e)
            {
                Log.Error("ssss", "Failed to open ParcelFileDescriptor", e);
            }
            return null;
        }
      }
    
    [Register("android/print/MyWriteResultCallback")]
    public class MyWriteResultCallback : PrintDocumentAdapter.WriteResultCallback
    {
        public MyWriteResultCallback(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }
    
        public override void OnWriteFinished(PageRange[] pages)
        {
            base.OnWriteFinished(pages);
        }
    }
    
  • Dinesh_OfficialDinesh_Official Member ✭✭✭
    edited January 2020

    Hi @LeonLu ,

    Thanks for the answer, i tried with your code. But it throws the exception

    I have attached a simple sample. Can you please look into it and help me to sort out the issue please?

  • Dinesh_OfficialDinesh_Official Member ✭✭✭

    @LeonLu , Please guide for five weeks i am looking for a solution.

  • Dinesh_OfficialDinesh_Official Member ✭✭✭

    @LeonLu ,

    Thanks for your suggestion. Finally, i can able to render the HTML file to PDF as required.

    I am sharing my git repo for future reference.

    https://github.com/dinesh4official/XFPDF

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    OK, thanks for your update.

  • wagenheimerwagenheimer Member ✭✭

    @Dinesh_Official

    Dinesh, your solution is the only one that is actually working! Thanks for it!
    I'm trying to integrate it into my project!

    What do you think of creating a Nuget package to make it easier to use? I'm sure it would be very popular, as there is NO other, not even PAID, available today.

    The solution from Forms9Patch is not working anymore and seems that the developer moved away from Xamarin and probably will not update it anymore.

  • wagenheimerwagenheimer Member ✭✭

    Well... For some reason, I wasn't able to make it work with my project. I only get an empty page on the final PDF.

Sign In or Register to comment.