Convert pdf to base64

Hello

How I can convert pdf to base64?

thank you

Here’s where you can have some fun with AppStudio’s new AI functionality. From the green ChatGPT button in the Code Windows, I did the following;

I’m assuming you have read the pdf into a string. If you need to figure out how to read it into a string, you have two questions. :slight_smile:

You can convert a string to Base64 in JavaScript using the btoa function. Here’s a simple example:

function stringToBase64(str) {
    return btoa(unescape(encodeURIComponent(str)));
}
// Example usage
const originalString = "Hello, World!";
const base64String = stringToBase64(originalString);
console.log(base64String); // Outputs: SGVsbG8sIFdvcmxkIQ==

Explanation:

  • encodeURIComponent: Encodes the string to ensure it’s safe for Unicode characters.
  • unescape: Transforms the encoded string back to its original form.
  • btoa: Converts the string to Base64.

This code will work in modern browsers.

How can I use inappbrowser with base64. My screen remains black. And I don’t know what to do anymore…

Thanks

Can you explain more about what you are trying to do?

Why are you using inappbrowser?

How are you using it?

Have you hooked up the debugger to check for error messages?

This is what I do. Starting with a pdf created with jsPdf.

opt = {"orientation": "p", "unit": "mm", "format": "a4", "putOnlyUsedFonts":"true"}
pdf = new jsPDF(opt) '
pdf.addPage()
pdf.addHTML(document.body, 20, 20, {pagesplit: False, margin: {top: 20, Right: 20, bottom: 20, Left: 20, useFor: "page"}}
blob = pdf.output("blob")
Emailreader.readAsDataURL(blob)
End Function

Function Emailreader_onload(e)
  console.log("Emailreader_onload")
  pdfname = "Consultation receipt " & Consult.appointmentId & "-" & Consult.clientId & ".pdf"
  fileName = ServerDataDir & "CustomerData/ConsultationReceipts/" & pdfname
  'this part is critical for when I send it as an attachment in phpMailer and I do believe also when I save it to an sql database'
  base64pdf = Replace(e.target.result,"data:application/pdf;base64,","")
'do whatever you want with base64pdf from here'
End function

When I attach it to an email in my php script I have to do this…

if (strlen($attachment) > 10)
{
    $img = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $attachment)); 
    $mail->AddStringAttachment($img, $filename);
} 

Not sure if that helps you at all.

Can you explain more about what you are trying to do?

I am trying to open a pdf with inappbrowser

Why are you using inappbrowser?

to publish an app on the play store you cannot use the
cordova.plugin fileopener2 as it uses the permission:request.install.package

How are you using it?

like in the example:

//'Base64 PDF files
var pdf1 = "data:application/pdf;base64,put your base64 code here";

Function pdf1_btn_onclick()
  cordova.inappbrowser.open(pdf1);
End Function

Function pdf2_btn_onclick()
  cordova.inappbrowser.open(pdf2);
End Function

Have you connected the debugger to check for error messages?
yes

Have you connected the debugger to check for error messages?
yes,error external link not found

So, this plugin?

What’s the actual message? Does it give the name of the file?

I have found that the in app browser plugin (Android at least) is not capable of opening PDF files.

I use this plugin: cordova-plugin-preview-any-file

It can also be another Cordova plugin to view pdf but can’t use request installer packages.

Thanks

Hello
It worked perfectly
I had to add manual url.cordova.file.externalRootDirectory don’t work. Manually yes…
Thanks a lot…