Download PDF using Code step in Zapier - javascript

I am trying to download a PDF from a website using Javascript in the "Code" step in Zapier. I am struggling to find documentation on how to output the file object. Here is my basic code so far. unfortunately, the file that comes back is garbage characters. Perhaps it needs to be encoded? I would also like any advice you might have on how I would modify this code if the file I wanted was on a website that required basic authentication. Any advice would be greatly appreciated:
fetch('http://www.pdf995.com/samples/pdf.pdf')
.then(function(res) {
return res.text();
})
.then(function(body) {
var output = {id: 1234, myPDF: body};
callback(null, output);
})
.catch(callback);

You're code should work fine. If you're doing something like sending an email or uploading to Dropbox, you can select "Run JavaScript" in the attachment or upload document option and choose your JavaScript action.
I've only been able to generate a PDF locally so far using res.buffer() instead of res.text() and then passing the buffer to fs.writeFile().

Related

How to save file from user to variable discord.js

I have been trying to get my discord bot to be able to accept a user input from a slash command prompt of drop-uploading files, and then write that file locally in my repl.it, but I have had no such success. So far, all I have been able to do is create an empty file with the same name as the file uploaded, but I can't seem to get fs() to correctly accept the input, sorry for the paragraph and possibly bad grammar, I have been up for 32 hours this round of coding. Any answers would be greatly appreciated. Also, tell me how I can improve my questions please.
Edit:
The following code does not work, when I try uploading the file, it writes undefined
message.attachments.forEach(a => {
fs.writeFileSync(`./${a.name}`, `${a.file}`); // Write the file to the system synchronously.
});
I created some working code from discordjs.org's guides, here it is
fs.appendFile(filePath, `${os.EOL}${args[1]}`, function (error) {
if (error) return log.error(error);

Read Excel sheet with Javascript

I would like to retrieve data from specific cells in a local Excel sheet using Javascript and then output it on an HTML page. My function would be something like readData(row, col).
For example:
Cell 1A = "hello"
<h4><script>readData(1, A)</script></h4> will show as "hello" on my page.
Every solution I have seen has involved uploading the file first (example below from the read-excel-file package), but my Excel file is already a local file within my project.
<script>
var input = document.getElementById('input')
input.addEventListener('change', function() {
readXlsxFile(input.files[0]).then(function(rows) {
// `rows` is an array of rows
// each row being an array of cells.
})
})
</script>
My guess is you are referring to this project?
https://gitlab.com/catamphetamine/read-excel-file
I tried using the Node.js example. It works out of the box. The examples for a direct website integration are broken. The project seems still alive, but I would scan the code to decide if to trust the author with your data.
Here how to fix the example (I have no idea for what cases it works!):
Go into the "website" folder. The index.html is a example how to use it. Unfortunately the "read-excel-file.min.js" is missing in the repo. You can download it here (view-source:https://catamphetamine.gitlab.io/read-excel-file/read-excel-file.min.js). Put it in the folder and it works.
Maybe someone has a better code for this?
EDIT (Above answer not technically wrong but does not answer the post):
What you want is not possible to security reasons! Just imagine your javascript (client!) code:
inputfile="test.xlsx"
I could edit this in my browser to
inputfile="super_secret_files_on_server.xlsx"
Solution: You could run node.js code on your server and solve it this way, or the user has manually upload a file from his computer. Or use a server side programming languages like php.

Javascript: Save content to Text File (No complicated Server stuff / HTTPRequest )

I am working on a small Project for myself. I wont upload a webpage but only run the files on localhost on my machine(HTML,CSS,JAVASCRIPT)
function saveData() {
$.post("testFile.txt", "New Content of the File ");
}
function getData() {
$.when($.get("testFile.txt")).done(function(requestedData) {
alert(requestedData);
});
}
Ok so this is basically a dummy Project to explain my Problem.
The methods are called by two different Buttons with an "onclick" event.
First Button which calls "getData()" works fine. The testFile.txt is located right next to the Html and Javascript file, and the getData() method correctly alerts the content of the text file as a String.
My Problem:
How do I make the saveData() method work? Do I misunderstand the $.post() method? (https://www.w3schools.com/jquery/jquery_ajax_get_post.asp)
I read so many forums and topics the last days but none of them solves the problem the way I´d need it, because I don´t wanna do all these difficult looking http-requests things - just reading and writing to this file that is located in the same folder then all my html and javascript Files
Thanks :)
$.post is for sending a POST http request.
Client-side javascript doesn't have permission to write to a client machine.
This would be a massive security hole if it did.
However
You've tagged node.js in your question, which can write to the local filesystem:
var fs = require('fs');
fs.writeFile("testFile.txt", "New Content of the File", function(err) {
//done
});

Check uploading file is password protected or not in Javascript or jQuery

Please help me on this.
I have some file upload functionality in my application which is implemented using Javascript/jQuery on the front-end and an AJAX call to send the file object to the back-end written in Java.
I need to detect if the file uploaded is password protected. If it is then I want to throw an error to the user.
Is there an option to check this on the front-end using Javascript/jQuery or should I do it in the back end?
In my Java middle-ware I am using the SmartUpload class to fetch the file object and insert it into a DB (blob format).
Please suggest which is better and why.
Thanks in advance.
Here is how you can check if a pdf file is encrypted or not in java.
See the sample below
try
{
document = PDDocument.load( "C:\\abc.pdf");
if(document.isEncrypted())
{
//Then the pdf file is encrypeted.
}
}

MHT DocumentViewer for HTML5/JS Mobile Application

I am developing a Mobile Application (primary for iPhone/iOS) with HTML5/JS (with the DevExtreme-Package from DevExpress). Now, I need to display the content of MHT-Files in this Application. For this I am searching for a document viewer which can display MHT-Files.
Optimal would be, if the control cannot only display MHT-Files rather the user can edit the content.
Can someone help me, how can I do that? Perhaps would it be possible to use a web-browser-control to view it?
MHT or Mime HTML is an archive format... so you should be able to convert to HTML then back to .mht...
You should be able to use https://github.com/zsxsoft/mhtml-parser to parse mht...
let parser = require('mhtml-parser');
parser.loadFile(__dirname + "/simple/simple.mht", {
charset: "gbk"
}, function(err, data) {
if (err) throw err;
console.log(data);
});
You should then be able to modify the file contents then you need to convert it back to mht which I'm not sure if above mentioned repo can do, Still you should be able to reverse engineer and create for yourself html to mht converter, when you do consider making it open source, so other people can use it... ;-)
I haven't used this personally, but it's got six stars... and seems to be most popular repo for this work...
Alternatively...
You can search mht on github to find more candidates suitable for the job ;-)

Categories

Resources