Convert base64 String into PDF file in Flutter - flutter

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.

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
...

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

Related

How to convert PDF file to DOC file in Flutter Web?

I am creating pdf file from pdf package in pub.dev, now I want to convert that pdf file to doc file so how to convert pdf to doc in flutter web?
https://pub.flutter-io.cn/packages/docx_template
Use this package if you wish to editthe doc file.
When you save the pdf you must be using
await pdf.save();
This will return a bytes array add that to the doc file like
final of = File('generated.docx');
await of.writeAsBytes(await pdf.save());

convert base64 encoded png pic using PIL

all. Recently I am using PIL to complete some code. As a default demo shows me how to convert a jpg picture file like this:
img = Image.open(filePath).convert('RGB')
Then I also try to convert a base64 encoded picture to finish the same thing. The only difference is the picture is obtained from url which is base64 encoded.
codec = 'base64 encoded picture'
base64_data = re.sub('^data:image/.+;base64,', '', codec)
byte_data = base64.b64decode(base64_data)
image_data = BytesIO(byte_data)
img = Image.open(image_data)
img = img.convert('RGB')
which shows "broken stream whille reading the picture" at the and of the last line. Is there any thing I misunderstand??
For some debugging work, I find that python2 and python3 use different lib(IOString and ByteIO). Just make a mismatch and correct it will make this code work.

Handling multiple encodings in a single file

I'm encountering some weird encoding issues. I need to parse an HTML document from the web, and I'm using the 'Content-Type' charset meta-data to determine the encoding type.
One page has been giving me trouble and is encoded by 'Shift_jis' (Japanese) - The parser result contains some garbled characters.
When I parse the same document using UTF-8 the characters that were garbled before are parsed correctly but everything else is now garbled.
I'm assuming the document contains text in two different encoding types.
I there anyway I could parse this document correctly ?
Also, I don't how, but all the browsers seem to deal well with the issue and are presenting the page nicely.
Would really appreciate any thoughts on this.
The page that I need to parse : http://ao.recruit.co.jp/form.html
First of all, what the browser sees is:
莨夂、セ讎りヲ
What is shown in rendered html is not the same because of the CSS text-indent: -9999px and the background image laid over it. But it's there. Removing them will show the text browser is seeing.
Out of the box, decoding as Shift-Jis should give you 莨夂、セ讎りヲ?, but if you want same results as in a browser, you should use a custom CharsetDecoder with IGNORE:
URL url = new URL( "http://ao.recruit.co.jp/form.html");
BufferedInputStream bis = new BufferedInputStream(url.openStream());
CharsetDecoder decoder = Charset.forName("Shift-Jis").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
Reader inputReader = new InputStreamReader(bis, decoder);
String result = IOUtils.toString(inputReader);
System.out.print(result);
This will give you same result as with browsers. Of course, it won't parse the text from the image file.

PDF file stored as BLOB, view in a webpage perl

I have a code that handles displaying a blob from a local Oracle database. I store both JPG and PDF files as blob. I could view the JPG file, but not the PDF. I have checked these
$self->content_type('image/jpg')
to
$self->content_type('application/pdf').
And the Blob does have data. I checked the length and it has "184546".
All I get when I click the link for the pdf file is a blank page with the title GETIMAGPAGE(application/pdf).
Any help or pointers would be greatly appreciated.
Also, How can we have the content_type to enable two different mime_types? For example in my case both image as well as pdf, depending on what we get?
File::MMagic can recognize the type of data using magic numbers.
use File::MMagic;
$magic = File::MMagic->new;
$self->content($blob);
$self->content_type($magic->checktype_contents($blob));
If you don't want to require a native/plugin PDF reader, perhaps FlexPaper might fit your needs.

iText: Insert pdf bytes into an opened document

Currently, our team is using Image.getInstance(bytes[]) to get an image object from png bytes. But later we will use a pdf file instead of png. So my question is that given pdf bytes, is there any way to convert it into png bytes or insert into opened document?
Something like this:
void insertIntoDocument(byte[] pdfBytes, Document document){}