Convert base64 String into PDF file in Flutter

2,746

Solution 1

Check this : https://stackoverflow.com/a/55599926/305135

(Following code is copied from link above)

This should convert base64 encoded pdf data into a byte array.

import 'packages:dart/convert.dart';

List<int> pdfDataBytes = base64.decode(pdfBase64)
  .map((number) => int.parse(number));

The pdf and the image plugins seems to suit your needs for displaying pdf.

The code should be roughly like so:

import 'package:pdf/pdf.dart';
import 'package:image/image.dart';

...
Image img = decodeImage(pdfDataBytes);
PdfImage image = PdfImage(
  pdf,
  image: img.data.buffer.asUint8List(),
  width: img.width,
  height: img.height);
// Display it somehow
...

Solution 2

At first i was doing the same thing like you. But i didnt file any appropriate solution to convert the base64 String into a pdf file.

I think you can get the BufferArray and then convert it into a pdf file. I have answered how to parse blob data to pdf in this question : How to convert ByteBuffer to pdf

Share:
2,746
Code Hunter
Author by

Code Hunter

Updated on December 29, 2022

Comments

  • Code Hunter
    Code Hunter over 1 year

    I am trying to show a PDF file. But PDF file I am receiving from server in Base64 String format. Is there any way I can directly show Base64 String into PDF viewer or WebView without saving it into File.

  • BosS
    BosS almost 3 years
    What is pdf parameter in PdfImage object?