A React Native module for accessing PDF data from the clipboard in Expo apps. This module provides a way to check if PDF content is available in the clipboard, retrieve the PDF data along with metadata, and clear the clipboard.
- Check if the clipboard contains PDF content
- Retrieve PDF content as base64 encoded string
- Get PDF metadata (title, author, page count, etc.)
- Clear clipboard content
- Cross-platform support (iOS and Android)
# Install the latest version
npm install github:JustinHaut/pdf-clipboard
# or
yarn add github:JustinHaut/pdf-clipboardIf you encounter module resolution issues after installation, try these solutions:
- Force TypeScript path mapping in your app's tsconfig.json:
{
"compilerOptions": {
"paths": {
"pdf-clipboard": ["./node_modules/pdf-clipboard/build"]
}
}
}- Create a local .npmrc file in your project root:
# .npmrc
public-hoist-pattern[]=pdf-clipboard
- Install from a specific git tag/commit (only if needed for stability):
# Only use this if you need a specific version
yarn add github:JustinHaut/pdf-clipboard#main- Use resolutions in package.json (yarn workspaces):
"resolutions": {
"pdf-clipboard": "github:JustinHaut/pdf-clipboard"
}npm install pdf-clipboard
# or
yarn add pdf-clipboard- Expo SDK 52 or higher
- iOS 14.0 or higher
- Android API level 21 or higher
import * as PdfClipboard from 'pdf-clipboard';
// Check if clipboard has PDF content
const checkForPdfContent = async () => {
try {
const hasPdf = await PdfClipboard.hasPDFContent();
console.log('PDF detected:', hasPdf);
} catch (error) {
console.error('Error checking clipboard:', error);
}
};
// Get PDF content from clipboard
const getPdfContent = async () => {
try {
const content = await PdfClipboard.getPDFContent();
if (content) {
console.log('PDF metadata:', {
pageCount: content.pageCount,
title: content.title,
author: content.author,
fileSize: content.fileSize
});
// Use the base64 data with a PDF viewer
const base64Data = `data:application/pdf;base64,${content.base64}`;
// Pass base64Data to your PDF viewer
}
} catch (error) {
console.error('Error getting PDF content:', error);
}
};
// Clear clipboard
const clearClipboardContent = async () => {
try {
await PdfClipboard.clearClipboard();
console.log('Clipboard cleared');
} catch (error) {
console.error('Error clearing clipboard:', error);
}
};You can use the retrieved PDF content with a PDF viewer like react-native-pdf:
import Pdf from 'react-native-pdf';
import * as PdfClipboard from 'pdf-clipboard';
// In your component
const [pdfUri, setPdfUri] = useState(null);
const getPdfContent = async () => {
try {
const content = await PdfClipboard.getPDFContent();
if (content) {
const base64Data = `data:application/pdf;base64,${content.base64}`;
setPdfUri(base64Data);
}
} catch (error) {
console.error('Error getting PDF content:', error);
}
};
// In your render
{pdfUri && (
<Pdf
source={{ uri: pdfUri }}
style={{ flex: 1, width: '100%', height: 400 }}
onLoadComplete={(numberOfPages) => {
console.log(`PDF Loaded: ${numberOfPages} pages`);
}}
onError={(error) => {
console.log('PDF Error:', error);
}}
/>
)}Checks if the clipboard contains PDF content.
Returns: Promise that resolves to a boolean indicating whether the clipboard has PDF content.
Gets the PDF content from the clipboard as a base64 string along with metadata.
Returns: Promise that resolves to a PDFMetadata object, or null if no PDF content is available.
The PDFMetadata object contains the following properties:
base64(string): Base64 encoded string of the PDF contentpageCount(number, optional): Number of pages in the PDFtitle(string, optional): Title of the PDF documentauthor(string, optional): Author of the PDF documentcreator(string, optional): Creator of the PDF documentfileSize(number, optional): Size of the PDF file in bytes
Clears the clipboard content.
Returns: Promise that resolves when the clipboard is cleared.
- Uses
PDFKitto extract metadata from PDFs - Requires iOS 14.0 or higher
- Uses content resolvers to extract PDF data from URIs
- Support may vary across different Android devices
MIT