3

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.

Code Hunter
  • 10,075
  • 23
  • 72
  • 102

2 Answers2

3

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
...
BlackPearl
  • 2,532
  • 3
  • 33
  • 49
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
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