send email with pdf attachment using logic app : PDF is not getting opened. It got corrupted - base64

Sending PDF in Base64 string, using json object. Converted Stream to Base64 before passing. Using Http trigger post for calling logic app
Email are generated with pdf from logic app but PDF is not opening. Is Base64 content corrupted?
In Logic app, updated attachment content by ContentBytes => triggerBody()['attachments']?['ContentBytes'] and ContentBytes => base64(triggerBody()['attachments']?['ContentBytes'])

After some testing, you should use the base64ToBinary expression:
base64ToBinary(triggerBody()['attachments']?['ContentBytes'])

Related

POST Multipart NodeJS with file JSON

I need to make a post using nodejs. I will send a text and a json file content to a url. Using insomia I can send normally (print below), but not in the code integration. Does anyone have an example?
SEND OK
ERROR

Unable to fetch blob data in correct format using node/react

I have a node.js/react application. I am trying to fetch Binary data(any document of type jpeg/png/pdf) and then show it in a new browser tab. From react I am using fetch and from Node.js using axios.
The API request is successful and I am getting back BLOB data but I see the data of type text/html instead of "image/jpeg". Please see below for BLOB details in the console:
size: 277453
type: "text/html"
Now even if I try to change the type to jpeg by using the code below the new tab that opens up shows a corrupt file instead of the actual image.
previewDocument()
.then((res) => {
console.log(res);
const blobWithType = res.slice(0, res.size, "image/jpeg");
const urlToPreview = URL.createObjectURL(blobWithType);
window.open(urlToPreview, "_blank").focus();
})
I tried fetcing arrayBuffer as well via axios and convert the same to Blob but getting similar results. If I do not set the type, I am seeing gibberish encoded data in the browser

Pdfmake in node.js to download pdf to flutter application

So what I exactly want to do is,
Generate a pdf for an order in my order management system.
I am using node.js as the backend and flutter as the front end.
What I had in my mind is: Generate the pdf and store it on the server, return the link to the pdf to the flutter app and then download the pdf, but I don't want to store the pdf on the server, so when my flutter app calls the generatePdf endpoint using GET method, how can I send the pdf directly? Do I need to use res.download ? I am new to node.js and cannot understand the flow for downloading the file. Also, how do I need to change the request in my flutter app for the same?
Note: I am using PDFMake to generate the pdf.
Pdfmake has an option to 'pipe' the generated doc directly to the client.
So, if you pass the response object to the code that generates the pdf, it will be something like:
var printer = new PdfPrinter(fonts);
//Assuming pdf is your generated PDF:
doc = printer.createPdfKitDocument(pdf);
res.setHeader('Content-type', 'application/pdf');
res.setHeader('Content-disposition', 'inline; filename="nice.pdf"');
doc.pipe(res);
doc.end();

Display PDF in html from nodejs server

I have a pdf file stored on my node.js/express server and I want to display it in my html page.
In my controller I have the following code : CONTROLLER
let filePath = process.env.uploads_dir+'/todolists/'+req.params.clid+'/'+req.params.siid+'/'+req.params.fileName;
res.download(filePath);
Then, in my html (pug) page I just have this code : (src contains the route that calls the controller) : PUG
block prepend body
body#page-top.fixed-nav.sticky-footer.bg-dark
.content-wrapper
.container-fluid
embed(width="400", height="400", src="/download/clientName/filename.pdf", type="application/pdf")
The pdf viewer shows me an error "Failed to load PDF file". I have already try to use PDFObject and PDF.js but still the same problem and I have also read some questions but nothing too.
QUESTIONS :
How to send a pdf file from Node/Express app to the browser
Failed to load pdf document, NodeJs

Image uploading - How to get the right format of image data for server side processing

I'm using sharp to process images on the server side and react dropzone to get the image file. When I post the file to the server from dropzone, I get the blob out of the request.body that looks like:
{ preview: 'blob:http%3A//127.0.0.1%3A3000/1451580a-b24f-478f-a9f7-338c0d790829' }
Optionally, before I send data to the server I can use FileReader (or something else) to do something with the image file instead of turning it into a blob.
Sharp takes:
A Buffer containing JPEG, PNG, WebP, GIF, SVG, TIFF
Raw pixel image data
A String containing the path to an image file, with most major formats supported.
How can I use what I have to provide sharp a supported format?
I recommend trying out a node.js module called Multer to help you access your photo file on your server.
https://github.com/expressjs/multer
First, on the client, you'll want to append your file to a FormData object like this:
// obtain the file from your react dropzone and save it to this file variable
const file = dropzone.file // not sure how you do this with react dropzone
const data = new FormData()
data.append('photo', file)
Then you'll send this FormData object to your server. On the server you'll use Multer on the route you're using for the photo for processing.
Make sure you npm install multer, and require it on your server or routes file. If you're sending a single file you'll use the multer 'single' method. If you want to do anything different check out the API documentation.
app.post('/photos', multer().single('photo'), controller.processPhoto);
In this example route, you're sending a POST request to /photos, multer is looking for a file with a FormData key of 'photo' and appending that to the request object.
Then in this made up 'controller.processPhoto' method you'll have access to the image as a property of the request object, on req.file. With this you can easily access a lot of good information including the image buffer req.file.buffer which it sounds like you need. (also mimetype, original name etc.)
This should be enough to get you started.

Resources