or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdconversion.mdindex.mdinput-sources.md

input-sources.mddocs/

0

# PDF Input Sources

1

2

pdf2pic supports three different ways to initialize PDF conversion, each accepting the same configuration options and returning identical Convert interfaces.

3

4

## Capabilities

5

6

### From File Path

7

8

Initialize PDF conversion by providing a file system path to the PDF document.

9

10

```typescript { .api }

11

/**

12

* Initialize PDF to image conversion from file path

13

* @param filePath - Path to the PDF file on the file system

14

* @param options - Configuration options for conversion

15

* @returns Convert interface for performing conversions

16

*/

17

function fromPath(filePath: string, options?: Options): Convert;

18

```

19

20

**Usage Examples:**

21

22

```typescript

23

import { fromPath } from "pdf2pic";

24

25

// Basic usage with minimal options

26

const convert = fromPath("/path/to/document.pdf");

27

const result = await convert(1);

28

29

// With custom options

30

const convert2 = fromPath("/path/to/document.pdf", {

31

format: "jpg",

32

width: 800,

33

height: 600,

34

savePath: "./output"

35

});

36

const result2 = await convert2(1, { responseType: "image" });

37

```

38

39

### From Buffer

40

41

Initialize PDF conversion from a Buffer containing PDF data, useful when working with PDF data in memory or from network requests.

42

43

```typescript { .api }

44

/**

45

* Initialize PDF to image conversion from Buffer

46

* @param buffer - Buffer containing PDF data

47

* @param options - Configuration options for conversion

48

* @returns Convert interface for performing conversions

49

*/

50

function fromBuffer(buffer: Buffer, options?: Options): Convert;

51

```

52

53

**Usage Examples:**

54

55

```typescript

56

import { fromBuffer } from "pdf2pic";

57

import fs from "fs";

58

59

// From file system buffer

60

const pdfBuffer = fs.readFileSync("/path/to/document.pdf");

61

const convert = fromBuffer(pdfBuffer, {

62

format: "png",

63

density: 150

64

});

65

const result = await convert(1, { responseType: "base64" });

66

67

// From HTTP response

68

const response = await fetch("https://example.com/document.pdf");

69

const buffer = Buffer.from(await response.arrayBuffer());

70

const convert2 = fromBuffer(buffer);

71

const result2 = await convert2.bulk([1, 2, 3]);

72

```

73

74

### From Base64

75

76

Initialize PDF conversion from a base64-encoded string containing PDF data, commonly used in web applications and API integrations.

77

78

```typescript { .api }

79

/**

80

* Initialize PDF to image conversion from base64 string

81

* @param b64string - Base64-encoded PDF data

82

* @param options - Configuration options for conversion

83

* @returns Convert interface for performing conversions

84

*/

85

function fromBase64(b64string: string, options?: Options): Convert;

86

```

87

88

**Usage Examples:**

89

90

```typescript

91

import { fromBase64 } from "pdf2pic";

92

93

// From base64 string (typical in web APIs)

94

const base64Pdf = "JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PC9UeXBlL..."; // truncated

95

const convert = fromBase64(base64Pdf, {

96

format: "jpg",

97

quality: 95,

98

width: 1024

99

});

100

101

// Convert to buffer for further processing

102

const result = await convert(1, { responseType: "buffer" });

103

const imageBuffer = result.buffer;

104

105

// Bulk conversion from base64

106

const allPages = await convert.bulk(-1, { responseType: "base64" });

107

allPages.forEach((page, index) => {

108

console.log(`Page ${index + 1} base64 length: ${page.base64.length}`);

109

});

110

```

111

112

## Input Source Comparison

113

114

| Input Source | Use Case | Advantages | Considerations |

115

|-------------|----------|------------|----------------|

116

| `fromPath` | Local files, server-side processing | Direct file access, efficient for large files | Requires file system access |

117

| `fromBuffer` | In-memory data, network streams | Memory-based, works with any data source | Memory usage for large PDFs |

118

| `fromBase64` | Web APIs, JSON payloads | Web-friendly, easy serialization | Base64 encoding overhead (~33% larger) |

119

120

## Common Patterns

121

122

### Error Handling

123

124

All initialization functions will validate inputs and throw errors for invalid parameters:

125

126

```typescript

127

import { fromPath } from "pdf2pic";

128

129

try {

130

const convert = fromPath("/nonexistent/file.pdf");

131

const result = await convert(1);

132

} catch (error) {

133

console.error("Conversion failed:", error.message);

134

}

135

```

136

137

### Option Inheritance

138

139

Options passed to initialization functions apply to all subsequent conversions, but can be modified via the Convert interface:

140

141

```typescript

142

import { fromPath } from "pdf2pic";

143

144

const convert = fromPath("/path/to/document.pdf", {

145

format: "png",

146

width: 800

147

});

148

149

// These will use png format and 800px width

150

await convert(1);

151

await convert.bulk([2, 3, 4]);

152

153

// Modify GraphicsMagick settings

154

convert.setGMClass("imagemagick"); // Use ImageMagick instead of GraphicsMagick

155

convert.setOptions(); // Apply any modified settings

156

```