React-Native convert a base64 images array to a single pdf - image

I'm working on a mobile app in react-native / redux in which I have to convert an array of base64 images into a pdf in order to send the pdf to the back-end.
Any idea about how to achieve it ?

Ok I finally found an easy solution with the help of react-native-image-to-pdf
It is promis based. In a file I called "pdfConverter.js" I created this function
import RNImageToPdf from "react-native-image-to-pdf";
export default base64Arr => {
// It is a promise based function
// Create an array containing the path of each base64 images
let base64Paths = [];
base64Paths.length = 0; // re-initialize the array for further re-use
base64Arr.forEach(base64 => {
base64Paths.push(`data:image/jpeg;base64,${base64}`);
});
// Convert base64 images to pdf from the paths array
return RNImageToPdf.createPDFbyImages({
imagePaths: base64Paths,
name: "PDF_Name"
});
};
and then call it where I need in another file :
import toPDF from "./pdfConverter.js";
toPDF(myBase64array)
.then(pdf => {
console.log("pdf ", pdf);
});

Related

Insert Image into a Google Sheet with Apps Script using base64 encoded data

Given some base64 encoded data for a png file, as in the example below from the image tag.
<img src="data:image/png;base64,R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7">
I need to create an image blob from the base64 encoded data for the insertImage() method.
sheetClass.insertImage(imageBlob, column, row)
Documentation:
Sheets Class - insertImage method
I've tried using the code below, but it throws the error:
Execution failed: Error retrieving image from URL or bad URL
The documentation states that the method needs a blob, not a URL, but it seems like it's expecting a link to an image file.
function insertImageFromBase64Src() {
var data = 'R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7';
var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), 'image/png').getBytes();
var ss = SpreadsheetApp.getActiveSpreadsheet();//This code is bound to a Sheet
var po = {
shName:'Update File',
column:1,
row:37
}
var sh = ss.getSheetByName(po.shName);
var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}
How about this modification?
Modification points:
insertImage can use the blob and URL which is the direct link of the image file.
In your script, imageBlob is an byte array which is "number[]". By this, I think that an error occurs.
Please add the name to the blob.
When the name is not given, an error occurs.
Modified script:
When your script is modified, it becomes as follows.
function insertImageFromBase64Src2() {
var data = 'R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7';
var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), 'image/png', 'sample'); // Modified
var ss = SpreadsheetApp.getActiveSpreadsheet(); //This code is bound to a Sheet
var po = {
shName:'Update File',
column:1,
row:37
}
var sh = ss.getSheetByName(po.shName);
var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}
In this modification, it supposes that data can be correctly decoded.
References:
insertImage(blobSource, column, row)
insertImage(url, column, row)
If this was not the direct solution of your issue, I apologize.

ABCPdf - Image not a suitable format

In the end, my goal is to send a raw image data from the front-end, then split that image into however many pages, and lastly send that pdf back to the front-end for download.
But every time I use the theDoc.addImageFile(), it tells me that the "Image is not in a suitable format". I'm using this as reference: https://www.websupergoo.com/helppdfnet/source/5-abcpdf/doc/1-methods/addimagefile.htm
To troubleshoot, I thought that the image might not be rendering correctly, so I added a File.WriteAllBytes to view the rendered image and it was exactly what I wanted, but still not adding to the PDF. I also tried sending the actual path of a previously rendered image thinking that the new image might not have been fully created yet, but it also gave me the same error. Lastly, I thought PNGs might be problematic and changed to JPG but it did not work.
Here is the code:
[HttpPost]
public IActionResult PrintToPDF(string imageString)
{
// Converts dataUri to bytes
var base64Data = Regex.Match(imageString, #"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
var binData = Convert.FromBase64String(base64Data);
/* Ultimately will be removed, but used for debugging image */
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string imgName= "Test.jpg";
string filename = Path.Combine(path, imgName);
System.IO.File.WriteAllBytes(filename, binData);
/***********************************************************/
using (Doc theDoc = new Doc())
{
// Using explicit path
theDoc.AddImageFile(#"C:\Users\User\Documents\Test.jpg", 1);
// Using variable
//theDoc.AddImageFile(filename, 1);
// What I really want
//theDoc.AddImageFile(binData , 1);
theDoc.Page = theDoc.AddPage();
theDoc.AddText("Thanks");
Response.Headers.Clear();
Response.Headers.Add("content-disposition", "attachment; filename=test.pdf");
return new FileStreamResult(theDoc.GetStream(), "application/pdf");
}
}
Try something like this (not tested, but cleaned up from my own code):
public int AddImageFile(Doc doc, byte[] data, int insertBeforePageID)
{
int pageid;
using (var img = new XImage())
{
img.SetData(data);
doc.Page = doc.AddPage(insertBeforePageID);
pageid = doc.Page;
doc.AddImage(img);
img.Clear();
}
return pageid;
}
To add a JPEG from a byte array you need Doc.AddImageData instead of Doc.AddImageFile. Note that AddImageFile / AddImageData do not support PNG - for that you would definitely need to use an XImage. The XImage.SetData documentation has the currently supported image formats.

MvcRazorToPdf How to download file using ajax call? MVC

I tried to use MvcRazorToPdf, i can able to generate mail merge letters. My aim is after downloading file i want to return json message from controller. how to do?
Controller: [how to convert the below lines to generate pdf and download without return]
return new PdfActionResult(pdfres, (writer, document) =>
{
document.SetPageSize(new Rectangle(PageSize.A4));
document.NewPage();
})
{
FileDownloadName = "ElanWasHere.pdf"
};
I appreciate your help. thanks

Getting file contents when using DropzoneJS

I really love the DropZoneJS component and am currently wrapping it in an EmberJS component (you can see demo here). In any event, the wrapper works just fine but I wanted to listen in on one of Dropzone's events and introspect the file contents (not the meta info like size, lastModified, etc.). The file type I'm dealing with is an XML file and I'd like to look "into" it to validate before sending it.
How can one do that? I would have thought the contents would hang off of the file object that you can pick up on many of the events but unless I'm just missing something obvious, it isn't there. :(
This worked for me:
Dropzone.options.PDFDrop = {
maxFilesize: 10, // Mb
accept: function(file, done) {
var reader = new FileReader();
reader.addEventListener("loadend", function(event) { console.log(event.target.result);});
reader.readAsText(file);
}
};
could also use reader.reaAsBinaryString() if binary data!
Ok, I've answer my own question and since others appear interested I'll post my answer here. For a working demo of this you can find it here:
https://ui-dropzone.firebaseapp.com/demo-local-data
In the demo I've wrapped the Dropzone component in the EmberJS framework but if you look at the code you'll find it's just Javascript code, nothing much to be afraid of. :)
The things we'll do are:
Get the file before the network request
The key thing we need become familiar with is the HTML5 API. Good news is it is quite simple. Take a look at this code and maybe that's all you need:
/**
* Replaces the XHR's send operation so that the stream can be
* retrieved on the client side instead being sent to the server.
* The function name is a little confusing (other than it replaces the "send"
* from Dropzonejs) because really what it's doing is reading the file and
* NOT sending to the server.
*/
_sendIntercept(file, options={}) {
return new RSVP.Promise((resolve,reject) => {
if(!options.readType) {
const mime = file.type;
const textType = a(_textTypes).any(type => {
const re = new RegExp(type);
return re.test(mime);
});
options.readType = textType ? 'readAsText' : 'readAsDataURL';
}
let reader = new window.FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = () => {
reject(reader.result);
};
// run the reader
reader[options.readType](file);
});
},
https://github.com/lifegadget/ui-dropzone/blob/0.7.2/addon/mixins/xhr-intercept.js#L10-L38
The code above returns a Promise which resolves once the file that's been dropped into the browser has been "read" into Javascript. This should be very quick as it's all local (do be aware that if you're downloading really large files you might want to "chunk" it ... that's a more advanced topic).
Hook into Dropzone
Now we need to find somewhere to hook into in Dropzone to read the file contents and stop the network request that we no longer need. Since the HTML5 File API just needs a File object you'll notice that Dropzone provides all sorts of hooks for that.
I decided on the "accept" hook because it would give me the opportunity to download the file and validate all in one go (for me it's mainly about drag and dropping XML's and so the content of the file is a part of the validation process) and crucially it happens before the network request.
Now it's important you realise that we're "replacing" the accept function not listening to the event it fires. If we just listened we would still incur a network request. So to **overload* accept we do something like this:
this.accept = this.localAcceptHandler; // replace "accept" on Dropzone
This will only work if this is the Dropzone object. You can achieve that by:
including it in your init hook function
including it as part of your instantiation (e.g., new Dropzone({accept: {...})
Now we've referred to the "localAcceptHandler", let me introduce it to you:
localAcceptHandler(file, done) {
this._sendIntercept(file).then(result => {
file.contents = result;
if(typeOf(this.localSuccess) === 'function') {
this.localSuccess(file, done);
} else {
done(); // empty done signals success
}
}).catch(result => {
if(typeOf(this.localFailure) === 'function') {
file.contents = result;
this.localFailure(file, done);
} else {
done(`Failed to download file ${file.name}`);
console.warn(file);
}
});
}
https://github.com/lifegadget/ui-dropzone/blob/0.7.2/addon/mixins/xhr-intercept.js#L40-L64
In quick summary it does the following:
read the contents of the file (aka, _sendIntercept)
based on mime type read the file either via readAsText or readAsDataURL
save the file contents to the .contents property of the file
Stop the send
To intercept the sending of the request on the network but still maintain the rest of the workflow we will replace a function called submitRequest. In the Dropzone code this function is a one liner and what I did was replace it with my own one-liner:
this._finished(files,'locally resolved, refer to "contents" property');
https://github.com/lifegadget/ui-dropzone/blob/0.7.2/addon/mixins/xhr-intercept.js#L66-L70
Provide access to retrieved document
The last step is just to ensure that our localAcceptHandler is put in place of the accept routine that dropzone supplies:
https://github.com/lifegadget/ui-dropzone/blob/0.7.2/addon/components/drop-zone.js#L88-L95
using the FileReader() solution is working amazingly good for me:
Dropzone.autoDiscover = false;
var dz = new Dropzone("#demo-upload",{
autoProcessQueue:false,
url:'upload.php'
});
dz.on("drop",function drop(e) {
var files = [];
for (var i = 0; i < e.dataTransfer.files.length; i++) {
files[i] = e.dataTransfer.files[i];
}
var reader = new FileReader();
reader.onload = function(event) {
var line = event.target.result.split('\n');
for ( var i = 0; i < line.length; i++){
console.log(line);
}
};
reader.readAsText(files[files.length-1]);

How do I base64 encode an image blob received from an API inside Ionic2?

I call the Dropbox API get_thumbnail method, it returns an image blob. I want to base64 encode this so that i can use it in an tag inside the html.
Trying to use the installed node_module called base64-img,
How can I import base64-img module in to my .ts file.
You can do something like
var base64Img = require('base64-img');
...
// use it like this
// Async
base64Img.img('data:image/png;base64,...', 'dest', '1', function(err, filepath) {});
// Sync
var filepath = base64Img.imgSync('data:image/png;base64,...', '', '2');

Resources