Langsung ke konten

Memulai

  1. Instal plugin

    Terminal window
    npm i @capgo/capacitor-pdf-generator
  2. Sinkronkan platform

    Terminal window
    npx cap sync
import { PdfGenerator } from '@capgo/capacitor-pdf-generator';
const receiptHtml = `
<html>
<body>
<h1>Capgo Store</h1>
<p>Thank you for your purchase.</p>
</body>
</html>
`;
const pdf = await PdfGenerator.fromData({
data: receiptHtml,
documentSize: 'A4',
orientation: 'portrait',
type: 'base64',
fileName: 'receipt.pdf',
});
if (pdf.type === 'base64') {
const link = document.createElement('a');
link.href = `data:application/pdf;base64,${pdf.base64}`;
link.download = 'receipt.pdf';
link.click();
}
await PdfGenerator.fromURL({
url: 'https://docs.capgo.app/invoice?id=123',
orientation: 'landscape',
documentSize: 'A4',
type: 'share', // membuka dialog berbagi native
fileName: 'invoice-123.pdf',
});
  • Sertakan CSS inline di string HTML Anda atau berikan baseUrl agar aset relatif dapat terselesaikan dengan benar.
  • Saat mengembalikan data base64 di mobile, pindahkan ke filesystem (misalnya, Capacitor Filesystem) sebelum berbagi.
  • Orientasi landscape ideal untuk tabel lebar, sementara portrait cocok untuk sebagian besar dokumen.
  • Pertimbangkan menggunakan font CSS scoped (misalnya, font sistem) untuk rendering yang konsisten di seluruh platform.