Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wking-io committed Dec 6, 2023
1 parent 725e658 commit aa75b98
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 386 deletions.
2 changes: 1 addition & 1 deletion app/routes/documentation.$product.$ref[.]pdf.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { LoaderArgs } from '@remix-run/node'
import fs from 'fs/promises'
import path from 'path'
import { pdf } from 'remix-utils/responses'
import invariant from 'tiny-invariant'
import { contentPath } from '~/lib/docs/fs.server.ts'
import { pdf } from '~/utils/responses.server.ts'

export { headers } from '~/components/layout/Content.tsx'

Expand Down
2 changes: 1 addition & 1 deletion app/routes/documentation.private.$product.$ref[.]pdf.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { LoaderArgs } from '@remix-run/node'
import fs from 'fs/promises'
import path from 'path'
import { pdf } from 'remix-utils/responses'
import invariant from 'tiny-invariant'
import { privateContentPath } from '~/lib/docs/fs.server.ts'
import { pdf } from '~/utils/responses.server.ts'

export { headers } from '~/components/layout/Content.tsx'

Expand Down
27 changes: 27 additions & 0 deletions app/utils/responses.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Create a response with a PDF file response.
* It receives a string with the PDF content and set the Content-Type header to
* `application/pdf; charset=utf-8` always.
*
* This is useful to dynamically create a PDF file from a Resource Route.
* @example
* export async function loader({ request }: LoaderArgs) {
* return pdf(await generatePDF(request.formData()));
* }
*/
export function pdf(
content: Blob | Buffer | ArrayBuffer,
init: number | ResponseInit = {},
): Response {
let responseInit = typeof init === 'number' ? { status: init } : init

let headers = new Headers(responseInit.headers)
if (!headers.has('Content-Type')) {
headers.set('Content-Type', 'application/pdf')
}

return new Response(content, {
...responseInit,
headers,
})
}
Loading

0 comments on commit aa75b98

Please sign in to comment.