Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created August 11, 2021 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansemannn/0daa788b9afdfb34a476a669b9b7ae07 to your computer and use it in GitHub Desktop.
Save hansemannn/0daa788b9afdfb34a476a669b9b7ae07 to your computer and use it in GitHub Desktop.
Titanium - iOS: Create a PDF from an image
import TiPDFMerge from 'ti.pdfmerge';
const win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
const btn = Ti.UI.createButton({
title: 'Trigger'
});
btn.addEventListener('click', () => {
// Create an image with green background and red border (to check if it matches the bounds)
const img = Ti.UI.createView({ backgroundColor: 'red', width: 2000, height: 1000 });
img.add(Ti.UI.createView({ width: 1800, height: 800, backgroundColor: 'green' }));
// Generate the PDF
const pdf = TiPDFMerge.pdfFromImage({ image: img.toImage() });
// Save it to a file
const file = Ti.Filesystem.getFile(Ti.Filesystem.applicationCacheDirectory, 'test.pdf');
file.write(pdf);
// Print the path for debugging purposes
console.warn(file.nativePath);
});
win.add(btn);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment