Creating PDF files

388 views
Skip to first unread message

Steve Garman

unread,
Oct 15, 2019, 7:28:41 PM10/15/19
to DroidScript
I needed something to take my mind off a project that was driving me mad.

As we get the occasional request for a way to write PDF files, I thought I'd have a go at starting an implementation of jsPDF
https://github.com/MrRio/jsPDF/blob/master/README.md

I haven't gone very far. Just a simple native app that uses a WebView to create a PDF file with a couple of words of text in it.

It's not something I have a need for at the moment but I thought if anyone is considering building their own implementation, this might be a useful place to start.

The only problem it addresses is that Android does not work with doc.save() so I've used app.WriteFile() to save the file instead.

If you want to try it, there is a jsPDF manual you can find fairly easily. I really don't know how easy it will be.

Steve Garman

unread,
Oct 16, 2019, 4:14:48 AM10/16/19
to DroidScript
I would have sworn I attached an spk to that message.

Hopefully it's attached now.
pdfCreateTest.spk

Jijo John

unread,
Oct 17, 2019, 5:15:12 AM10/17/19
to DroidScript
Thank you
Message has been deleted
Message has been deleted

GT_CBG

unread,
Feb 9, 2024, 5:06:08 AM2/9/24
to DroidScript
Has anyone has any success with using jsPDF and doc.addImage() to add an image to the PDF?

On Thursday 17 October 2019 at 10:15:12 UTC+1 Jijo John wrote:
Thank you

Cemal

unread,
Feb 18, 2024, 7:13:11 AM2/18/24
to DroidScript
I encountered some problems but I managed to record it this way:

doc.text('Hello from DroidScript!', 20, 100);
doc.addImage(app.ReadFile("/Sys/Img/Hello.png", "base64"), 100, 50);

const arrayBuffer = doc.output("arraybuffer");
   
const file = app.CreateFile(outFile, "rw");
file.WriteData(new Uint8Array(arrayBuffer));
file.Close();
   
app.OpenFile(outFile);

You can also load jspdf directly with DroidScript. Download the latest version here in the folder with your project: https://unpkg.com/jspdf/dist/jspdf.umd.min.js
You can then use it this way:

app.Script("./jspdf.umd.min.js", true);

const doc = new jspdf.jsPDF();
const outFile = "./output.pdf";

function OnStart() {
  var lay = app.CreateLayout("linear", "VCenter,FillXY");

  var btn = app.CreateButton("Create PDF file");
  btn.SetOnTouch(btn_OnTouch);
  lay.AddChild(btn);

  app.AddLayout(lay);
}

function btn_OnTouch() {
  doc.text("Hello from DroidScript!", 20, 100);
  doc.addImage(app.ReadFile("/Sys/Img/Hello.png", "base64"), 100, 50);

  const arrayBuffer = doc.output("arraybuffer");

  const file = app.CreateFile(outFile, "rw");
  file.WriteData(new Uint8Array(arrayBuffer));
  file.Close();

  app.OpenFile(outFile);
}

GT_CBG

unread,
Feb 19, 2024, 4:19:32 PM2/19/24
to DroidScript

Hi Cemal, thank you very much, this has been so helpful!

The previously suggested approach with app.WriteFile() works for text added to a PDF but not for images.

I believe your approach works for PNG but not for JPEG, but it is absolutely fine for what I need, thanks again.

Giovanni


Reply all
Reply to author
Forward
0 new messages