Pdfprint function


#1

I tried using pdfprint function in an android apk…it does not work, but works fine in the pc browser. Anything that needs to be done to have it work in an android device?


#2

Have you hooked up the Chrome Remote Debugger to check for error messages?


#3

Not getting any error messages apk works perfectly but does not create pdf file as it does in pc browser.


#4

Yes I reported the same on Oct19 Email a screenshot The stock print2PDF sample doesn’t work for some reason.


#5

It’s probably because the browser is restricted from affecting the device’s filesystem. If you want to save a file on a device, you’ll need to use a PhoneGap plugin.

Another solution would be to send the pdf somewhere. Here’s a excerpt from a project which emails a completed form:

btnPreviewSendAsEmail.onclick = (() => {
  'use strict';

  const options = {};
  options.windowWidth = '900px';
  txtPreviewSellerEmail.innerText = 'gh@nsbasic.com';

  html2canvas(previewBody, options).then((canvas) => {
    cordova.plugins.email.open({
      to: txtPreviewSellerEmail.innerText,
      subject: 'Signed Agreement',
      body: 'See attached.',
      attachments: `base64:icon.png//${canvas.toDataURL().substr(22)}`,
    });
  });
});

This uses the html2canvas library:
https://html2canvas.hertzen.com/


#6

Tested pdfprint function as a pc windows.exe app. File downloads as undefined file. One has to rename file from undefine to undefine.pdf in order to open as a pdf file. Anyway around that.


#7

I used pdf.save(“mypdf”) where mypdf is the name to save it as.

When I tessted with the sample in Chrome on PC and on Android it worked. But does anyone know why it works? I thought you could not write anything to your local drive.

Thanks, John


#8

Android and Chrome allow it in limited circumstances. Not iOS.

https://caniuse.com/#feat=filesystem


#9

Since jsPDF is able to write to the filesystem (in the specified browsers as noted in your link), do you know of any other js scripts that can write to the filesystem, specifically text files? I’ve used phonegap to read/write files but am developing pure web apps that it would be nice to do the same.

Thanks, John


#10

The idea of a random web page being able to write text files to your computer is a bit scary, so it’s not allowed by the browsers.


#11

One of the other developers here had some code that might be of interest to this thread:

The cordova-pdf-generator plugin page is here:

In config.xml I specified the plugin like this:

<plugin name="cordova-pdf-generator" source="npm" />

You need to specify CSS files like below. I’ve tested it on an Android device and it works fine, but not sure if it works on iOS. If you see a blank result, it’s most likely a problem in CSS file.

frmHerdReport.sendPDF = (() => {
  'use strict';

  const fileName = 'myPdfFile.pdf';
  const emailAddress = '[billg@gmail.com](mailto:billg@gmail.com)';
  const options = {
    documentSize: 'A4',
    landscape: 'portrait',
    type: 'base64',
  };

  // list CSS files here
  // iOS: www/<css-folder>/<your-file.css>
  // Android: [file:///android_asset/www/<css-folder>/<your-file.css>](file:///android_asset/www/%3Ccss-folder%3E/%3Cyour-file.css%3E)

  let head = `<head>;
    <link rel="stylesheet" href="file:///android_asset/www/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="file:///android_asset/www/node_modules/datatables.net-bs/css/dataTables.bootstrap.css"/>
    </head>`;

  // in case of iOS
  if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
    head = head.replace(/file:\/\/\/android_asset\//g, '');
  }

  const body = `<body>${document.getElementById('reportCombinedTable').outerHTML}</body>`;

  cordova.plugins.pdf.fromData(`${head}${body}`, options)
    .then((base64) => {
      cordova.plugins.email.open({
        to: emailAddress,
        subject: 'Monthly Report',
        body: 'See attached.',
        attachments: `base64:${fileName}//${base64}`,
      });
    })
    .catch(err => alert(err));
});